SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Clearing a FORM if new ID

    I have a save.php page that will accept an $id from an edit.php form and change the information for the current record. If the $id is not passed the save.php is supposed to create a new record with a new unique $id.

    I am able to get the information to save correctly but everytime I pass no $id variable to my edit.php?id= page all of the fields in the form are filled with error HTML like:

    <br /><b>Notice</b>: Undefined variable: id_name in <b>/var/www/html/edit.php</b> on line <b>96</b><br />

    I can clear out the HTML errors and replace it with good info and it will save correctly.

    I'm know it's supposed to do this, I just need help figuring out how to automatically clear the form so all the HTML errors are not displayed.

    Any help would be great!

    Digit

  2. #2
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, my interpretation of what you've written suggests

    I would
    PHP Code:
    // assume your POSTing
    if(isset($_POST['id'])) {
        
    // blah - get data whatever from database
    }
    else {  
       
    // id is not set therefore clear form


  3. #3
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are correct, however I am new to php and I'm not sure how to clear the form if the id is not set:

    else {
    // id is not set therefore clear form
    }[/PHP]
    Can anyone recommend a good book or website?

    Digit

  4. #4
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha.. ok - to be honest when I'm stuck with PHP I hit google and just search for what I want help with, as there a MANY MANY MANY php resources. This site infact is a good one.

    Can you show your edit.php page as there are many ways this could be done, depending on what you've done!

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would simply say initialize your variables.

    If you use $id_name to add a name to a field, near the top of the script somewhere, you should have $id_name = '';

    That way if nothing is pulled from the database, $id_name will be defined as an empty string. The input box on the form will be set to an empty string and you won't get any errors.

  6. #6
    SitePoint Enthusiast duckax's Avatar
    Join Date
    Aug 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually on most servers, those error messages should be disabled so your code should work fine. However, this is not the best way to code :P

    I think you are doing something like

    PHP Code:
    <input type="text" value="<? print $_POST['id']; ?>">
    Change it to

    PHP Code:
    <input type="text" value="<? if(isset($_POST['id'])) print $_POST['id']; ?>">

  7. #7
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by duckax
    Change it to

    PHP Code:
    <input type="text" value="<? if(isset($_POST['id'])) print $_POST['id']; ?>">
    Thanks for the tip, it worked great!

    Digit


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
  •