SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    thane
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Submit button doesnt work

    Hi all,

    I am running Apache 2 on Windows XP, installed with PHP 4 and MySQL 3.23

    Below is a little code which i gathered for taking information from a form and putting it in a MySQL database.


    My problem is that i never get the "Thank You" message, which i should, and the data isnt inserted in the database. In fact, the page never enters the if($submit) loop itself when the submit button is clicked.


    What could be the problem? Please help.


    <html>
    <body>

    <?php
    if($submit) {
    $db = mysql_connect("localhost", "root", "passwd" );
    mysql_select_db("mydb",$db);
    $sql = "INSERT INTO emp (first,last,address,position) VALUES ('$first', '$last', '$address', '$position')";
    mysql_query($sql);
    echo "Thank You\n";
    } else {
    ?>

    <form method="post" action="<?php echo $PHP_SELF?>">
    First name : <input type="Text" name="first"><br>
    Last name : <input type="Text" name="last"><br>
    Address : <input type="Text" name="address"><br>
    Position : <input type="Text" name="position"><br>
    <input type="Submit" name="submit" value="Enter">
    </form>

    <?php
    }
    ?>

    </body>
    </html>

  2. #2
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vaibhav
    $db = mysql_connect("localhost", "root", "passwd" );
    Try...
    PHP Code:
    $db mysql_connect("localhost""root""passwd" )  or die(mysql_error()); 
    Your code as posted worked for me, so my guess is you're having trouble connecting to the database.
    Lats...

  3. #3
    SitePoint Member
    Join Date
    Jan 2004
    Location
    thane
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but no, its not a problem connecting to the database. I am able to connect to the database to retrieve data. I tried adding or die() statement but i still get the blank form again when I click the button.

    The problem is that the page never enters into the if($submit) loop itself.
    For example, even if I were to put an echo statement in the first line of the if() loop, the echo would still not be displayed. The page always enters the else{} loop only.

    Could you please guess what the problem might be.

    I even tried
    PHP Code:
    if(isset($_POST['submit'])) 
    as i'd seen in another tutorial, but still am not able to get it into the if() loop.

  4. #4
    SitePoint Zealot ssttoo's Avatar
    Join Date
    Jan 2004
    Location
    LA, California
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no problem with your code. I even pasted it an tried it on my machine, because I couldn't see anything wrong.
    If the code is a part of a more complex page, try looking for the problem elsewhere.

    As a side note - avoid the
    PHP Code:
    if ($submit
    aproach.
    Depending on your server configuration, somebody might overwrite your $submit variable using the URL or you might even overwite it yourself unvoluntarily.
    Safer option is what you suggested:
    PHP Code:
    if (isset($_POST['submit'])) 
    or
    PHP Code:
    if (!empty($_POST['submit'])) 
    or in this case even
    PHP Code:
    if (!empty($_POST)) 
    Best of luck!

  5. #5
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you copy and paste the code off of the internet or another source. If so, try retyping your code. I have had that problem in the past. I copied and pasted some code and it wouldn't work. Then i retyped the EXACT code and it worked perfectly!

    Just a suggestion
    ERiC

  6. #6
    SitePoint Zealot DarkWulf's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another possibility is that register globals is off.

    See if $_GET['submit'] or $_POST['submit'] is set?


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
  •