Pop-up popping too quickly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

Pop-up popping too quickly

Post by yoji »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I am trying to create a shoutbox application from ground up. Might seem a small project but I just in the practice phase right now. Anyways I am facing this problem.. First lemme show you the code:

Code: Select all

 
<html>
<body>
<form id="form1" name="form1" method="post" action="">
  <label>Name
  <input type="text" name="name" />
  </label>
  <p>
    <label>Last Name
    <input type="text" name="lastname" />
    </label>
  </p>
 
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
</form>
<?php
$host="localhost";
$user="root";
$pass="";
$db="my_db";
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$con = mysql_connect($host,$user,$pass);
if (!$con)
{
    die('couldnt connect'.mysql_error());
}
mysql_select_db ($db,$con);
$lengthError="
<script type=\"text/javascript\">
alert(\"Please fill out the form properly. First Name And Last Name should be more than 2 characters\")
</script>
 
";
if (strlen($name)<2 or strlen($lastname)<2)
{
    echo $lengthError;
}
else
{
$que="INSERT INTO persons (FirstName, LastName)
VALUES('$name','$lastname')";
mysql_query($que,$con);
}
 
$printQuery=mysql_query("SELECT * FROM persons");
 
while ($row=mysql_fetch_array($printQuery))
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br />";
}
mysql_close($con);
?>
</body>
</html>
 

The problem is that whenever I open this page it shows that javascript error on starting. It is only meant when somebody clicks on submit button and the forms are empty. Could anybody suggest me how to avoid this window to pop when I open the page...


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Pop-up popping too quickly

Post by aceconcepts »

You need to check whether the submit button has been clicked - like so:

Code: Select all

 
if(isset($_POST['submit']))
{
//validation etc...
 
$lengthError="
<script type=\"text/javascript\">
alert(\"Please fill out the form properly. First Name And Last Name should be more than 2 characters\")
</script>
 
";
}
 
Post Reply