Member Avatar for Rahul47

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.

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" />

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..

commented: Nice explanation +9
Member Avatar for Rahul47

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 :-)

glad you resolved your problem. I'm learning PHP as I go as well...

commented: Kudos !! +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.