Jump to content

How do i stop this simple form from submitting if a field is blank?


josuenerd

Recommended Posts

How do I stop the below form from submitting if any field or atleast if field Weight is blank?

 

also an error echo would be great.  :D  I have been trying to figure this out but cant do it.

 

<table width="500" border="0" align="center" cellpadding="10" cellspacing="0">
  <tr>
    <td>
    <form action="insert.php" method="post">
<input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" />
First Name: <input type="text" name="Firstname" /><br><br>
Last Name: <input type="text" name="Lastname" /><br><br>
Weight: <input type="text" name="Weight" /><br><br>

<input type="submit" value="Submit Profile" />
</form>
</td>
  </tr>
</table>

Link to comment
Share on other sites

Just for notification take out the:

 

header("location:index.php");

 

 

Once you send something to be printed on the document it has already sent the header therefore it will throw and error and the above script will now work.

Other than that it would work fine. Also consider looking into a 3rd party javascript for validation script.

Thanks,

Colton Wagner

Link to comment
Share on other sites

try

 

if($_POST['Weight']==""||$_POST['Firstname']==""$_POST['Lastname']==""){echo "error!";header("location:index.php")}else{[i]code if form is complete..[/i]}

 

assuming your form name is index.php..

 

cheers! :D

 

 

 

I don't know where to add that code :(

 

Below is the script:

 

insert.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Weight Log</title><style type="text/css">body,td,th {    font-family: Arial, Helvetica, sans-serif;    font-size: 12px;}body {    background-color: #EEEEEE;}</style></head><body><?php$con = mysql_connect("localhost","learnu","asdfasdf");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("learning", $con);$sql="INSERT INTO Peoples (Firstname, Lastname, Weight, Date)VALUES('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "1 record added";$db = mysql_connect("localhost","learnu","asdfasdf");mysql_select_db("learning",$db);$result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC");$table = '<table width="608" border="0" align="center" cellpadding="10" cellspacing="0">  <tr>    <td style="color:#fff;" width="207" bgcolor="#425d74"><strong>Firstname</strong></td>    <td style="color:#fff;" width="204" bgcolor="#425d74"><strong>Lastname</strong></td>    <td style="color:#fff;" width="197" bgcolor="#425d74"><strong>Date</strong></td>    <td style="color:#fff;" width="197" bgcolor="#425d74"><strong>Weight</strong></td>  </tr>';while($row=mysql_fetch_array($result)){  $firstname = $row['Firstname'];  $lastname = $row['Lastname'];  $weight = $row['Weight'];  $date = $row['Date'];   $table.= '<tr>';   $table.= '<td bgcolor="#e8f4ff">'.$firstname.'</td>';   $table.= '<td bgcolor="#e8f4ff">'.$lastname.'</td>';   $table.= '<td bgcolor="#e8f4ff">'.$date.'</td>';   $table.= '<td bgcolor="#e0ffe6"><strong>'.$weight.'</strong></td>';   $table.= '</tr>';}$table .= '</table>';echo $table;mysql_close($con)?> <?php$today = date("F j, Y");?> <?php$goalweight = 140;    $losepounds = $weight - $goalweight;  //we multiply varaible a and b and store the value in cecho $tablegoal;echo '<table width="500" border="0" align="center" cellpadding="15" cellspacing="0">  <tr>    <td align="center">You need to lose <strong>';    echo $losepounds;echo '</strong> pounds to reach your Goal Weight of <strong>';echo $goalweight;echo '</strong>.</td>  </tr></table>';?> <form action="" method="post"><input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" /><table width="312" align="center">    <tr>        <td width="141">        My weight is:</td>        <td width="42"><input type="text" name="Weight" size="7" maxlength="3" /></td>        <td width="113"><input type="submit" value="Change Weight" /></td>    </tr></table></form></body></html>

 

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.