josuenerd Posted October 12, 2010 Share Posted October 12, 2010 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. 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 https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/ Share on other sites More sharing options...
geekisthenewsexy Posted October 12, 2010 Share Posted October 12, 2010 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! Link to comment https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121380 Share on other sites More sharing options...
Colton.Wagner Posted October 12, 2010 Share Posted October 12, 2010 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 https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121436 Share on other sites More sharing options...
Faks Posted October 12, 2010 Share Posted October 12, 2010 http://www.benjaminkeen.com/software/php_validation/index.php here you can find what you was searching for works good . Link to comment https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121444 Share on other sites More sharing options...
rwwd Posted October 12, 2010 Share Posted October 12, 2010 if(($_POST['Weight'] == "") || ($_POST['Firstname'] == "") || ($_POST['Lastname'] == "")) Parenthesize what your evaluating... Makes it easier to see what's what. Rw Link to comment https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121446 Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 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! 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 https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121536 Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 http://www.benjaminkeen.com/software/php_validation/index.php here you can find what you was searching for works good . This looks great but I don't have any idea how to implement this into my script. Looks like alot of code on the demo.php Can you help me implement this validation script to my form? Link to comment https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121538 Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 http://www.benjaminkeen.com/software/php_validation/index.php here you can find what you was searching for works good . I figured out how to use this Thanks! Link to comment https://forums.phpfreaks.com/topic/215679-how-do-i-stop-this-simple-form-from-submitting-if-a-field-is-blank/#findComment-1121571 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.