0

What is possibly wrong with following script that caused it to scream ?

<!DOCTYPE html>
<html>
<head>
 <script>
   function formSubmit()
    { 
        document.getElementById("form1").submit();
     }
 </script>
</head>
<body>
<form id="form1" action="test.php" method="POST">
 Name: <input type="text" name="name" ><br/>
 <input type="button" onclick="formSubmit()" value="Submit" />
</form>
<?php
$name1=$_POST['name'];
echo "<br> Your name is $name1 . Thanks for using our system.";
?>
</body>
</html>

Following is error snap:

a236c0c5cf354cbc5b176c1101c6b48b

Thank You.

3
Contributors
4
Replies
19
Views
4 Years
Discussion Span
Last Post by JorgeM
1

The $_POST array doesn't contain a name element. The reason is in that the name input field has not been submitted yet. Include a check such as:

<?php
if(isset($_POST['name']) && !empty($_POST['name'])) {
    $name1=$_POST['name'];
    echo "<br> Your name is $name1 . Thanks for using our system.";
}
?>

Even better would be to check for form submission:

<?php
if(isset($_POST['submit']) && isset($_POST['name']) && !empty($_POST['name'])) {
    $name1=$_POST['name'];
    echo "<br> Your name is $name1 . Thanks for using our system.";
}
?>

In order this to work you should add a name attribute to the submit button:

<input type="button" onclick="formSubmit()" name="submit" value="Submit" />

Edited by broj1

3

I'm not a PHP developer..I'm sure you'll have other members provide feedback.. but it seems to me that you only have one page and I guess that's Ok, but when this page loads, you are trying to assign a value which is probably undefined at the moment to $name1.

$name1=$_POST['name'];

After the form is submitted to the same page, I would imaging that $_POST['name'] would contain a value. Until the post happens, there is no value.

Does this seem correct?

[EDIT] -- looks like broj1 posted a response while I was typing mine. Thanks for validating what I was thinking..

Edited by JorgeM

Votes + Comments
Nice explanation
0

thanx fellas JorgeM and broj1 . . . . that helped me understand this topic a lot. Moving on to next problem hope i will get your help.

Thanx a ton :-)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.