Jump to content

submitting form data to a PHP script


Munali

Recommended Posts

Kindly help. Am a new php programmer and am learning how to HTML form data to a php script.

Have tried several times but information on html form never get submitted. For some unknown  reasons, the codes are failing . Here are my codes:

<html>
   <head>
       <title>Form Destination</title>
   </head>
   <body>
   <?php
    if(isset($_POST['submit']))
       {
           echo "Name: {$_POST['name']} <br/>";
           echo "Age : {$_POST['age']} <br/>";
           echo "Gender:{$_POST['gender']} <br/>";
           echo "Email:{$_POST['email']} <br/>";              
       }
       else
       {
           echo "Form was not submitted";  
       }
    ?>
   </body> 
</html>

 

and 

 

<html>
  <head>
      <title>Form Source</title>
  </head>

<body>
  <form  action="form_destination.php" method="post">
       <br/><br/>
    Name: <input type="text" name="name"/>
       <br/><br/>
    Age:&nbsp;&nbsp;&nbsp;<input type="text" name="age"/>
       <br/><br/>
    Gender: &nbsp;Male<input type="radio" name="gender" value="male"/>
    Female<input type="radio" name="gender" value="female"/>
       <br/><br/>
     Email:&nbsp;<input type="text" name="email"/>
       <br/><br/>
     <input type="submit" value="submit"/>
     
  </form>
</body>
</html>

Link to comment
Share on other sites

Your output depends on there being an input with the name "submit". There isn't one. As it is a bad idea to

  1. name an input "submit"
  2. rely on a button name to check for submissions of data (if you hit return the button name may not be sent depending on the browser)

A better approach to test for posted data is
 

if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
    // process posted data
}

 

  • Like 1
Link to comment
Share on other sites

The challenge with PHP is that you need to understand HTML, PHP and SQL to get anythign useful done and if you are shaky on any of that it is hell to track down problems.  Your HTML is shaky and that suggests you might need some more time on that before you move on but having said that your code looks fine apart from the issue already mentioned by Barand.  It should work once you remove the if statement.

If you want to have another go here is a page I wrote on the subject for 16 year olds.  The site was written to be followed start to end but I think the page makes sense on it's own (especially if you follow the link to the place I explained how to create a form.

https://www.yourwebskills.com/page.php?page=124

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.