SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an add function in a Content Management Systeem...

    People can either add a new 'rubriek' (section) or select an exsisting one from an options list/menu...

    The code looks like this:
    PHP Code:
    if (isset ($subcatid)){
        
    $subcatid $subcatid;
        print     
    "<html><head><title>Add rubriek</title></head>";
        print     
    "<form name='addform' method='POST' action='add1.php'>";
        print     
    "Title of 'rubriek' to add: <b>";
        print     
    "<input type='text' name='rubriektitle' size='40'><br>";
        print     
    "<select name='select'><option>Or, select an exsisting 'rubriek'...</option>";

        
    $sql     "SELECT * FROM rubrieken ORDER BY titel";
        
    $result mysql_query($sql);
        while (
    $row mysql_fetch_array($result)) {
                 
    extract($row);
               print 
    "<option value='$id'>$title</option>";
        }
        print     
    "</select>";
        print     
    "<input type='hidden' name='subcatid' size='1' value='$subcatid'>";
        print     
    "<body onload=\"document.addform.rubtitel.focus();\">";


    The problem is that I want to distinguish between something entered in the 'textbox' (rubriektitle) or when something is selected from the LIST/MENU. It is either textbox OR list/menu.
    How can I make this distinction... and which variable will be passed on when the list/menu has a selected item?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When your form is posted to add1.php the form elements will be available to you in php as variables. The variable names will be the names you gave each element in your form.

    Thus the text in the textbox will be in the variable $rubriektitel.
    Also a variable $select will contain the value you assigned to the option which was selected in the form.

    Does that help matters?

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know that $rubriektitel will be passed on to add1.php.
    I've used it to add, successfully, a new 'rubriek' to the database...
    In table 'pages' there will be a new 'rubriek' added to pages.rubrieken. This works successful.

    However... I also want the option to add an exsisting 'rubriek' to the pages.rubrieken...

    If I run the script from add1.php it will add $rubriektitel to the pages.rubrieken but als $select... which is empty. (Nasty database errors will occur)

    So, how do I make the distinction between a value entered in the 'new' textfield ($rubriektitel)... or one in the exsisting 'old' list/menu ($select)

    The code of add1.php:
    PHP Code:
    elseif (isset ($subcatid)){
        
    $check  "SELECT * FROM pages WHERE subcat = '$subcatid'";
        
    $res    mysql_query($check);
        
    $num     mysql_numrows($res);
            
        if (
    $num != '0'){
            
    $pageid mysql_result($res,0,'id');
            
    $sql    "INSERT INTO rubrieken(id,titel) VALUES('','$rubtitel')";
            
    $result mysql_query($sql);
            
            
    $sql1   "SELECT * FROM rubrieken WHERE titel = '$rubtitel'";
            
    $result1mysql_query($sql1);
            
    $rubid  mysql_result($result1,0,'id');
            
            
    $sql2   "SELECT rubrieken FROM pages WHERE id = '$pageid'";
            
    $result2mysql_query($sql2);
            
    $rubriekmysql_result($result2,0,'rubrieken');
            
            
    $rubids $rubriek ',' $rubid;
                    
            
    $sql3   "UPDATE pages SET rubrieken='$rubids' WHERE subcat =$subcatid";
            
    $result3mysql_query($sql3);
            print   
    " Rubrieken $rubids is toegevoegd aan Page $pageid, samen met rubrieken $rubriek";
        }
        else {     
            
    $sql    "INSERT INTO rubrieken(id,titel) VALUES('','$rubtitel')";
            
    $result mysql_query($sql);
            
    $subcatid $subcatid;
        
            
    $sql2   "SELECT * FROM rubrieken WHERE titel = '$rubtitel'";
            
    $result2mysql_query($sql2);
            
    $rubid  mysql_result($result2,0,'id');
        
            
    $sql3   "INSERT INTO pages(id,subcat,txt,rubrieken,datum) VALUES('','$subcatid','','$rubid','')";
            
    $result3mysql_query($sql3); 
            print 
    "Rubriek <b>$rubtitel$subcatid, rubid: $rubid</b> is correct opgeslagen";
        }



  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can test to see if any of the variables returned from the form are empty or not. In PHP an empty string (and zero) will evaluate to FALSE and a non-empty string (or a non-zero number) will evaluate to TRUE.
    PHP Code:
    if($select) {
       
    // the user has selected an existing rubrieken because $select has a value
    } else {
       
    // the user did not select any of the options
    }

    if (
    $rubriektitel) {
       
    // the user has entered text into the text field
    } else {
       
    // The user did not enter any text.
       // This assumes that in your form you did not give the text area a
       // default value otherwise $rubriektitel might still hold that default value

    Last edited by freakysid; May 21, 2001 at 07:02.

  5. #5
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for your help, Freakysid...

    this will help a lot...
    Just so you know, the Content Management System I've been working on the last few weeks is almost completed...
    the problem we've been talking about here in this thread is one of the last problems I have to solve...



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •