Local Tech Repair: Checking form submit with php

Saturday, January 29, 2011

Checking form submit with php

so one thing that took me a while to figure out is how to have php check the form submissions in the same page but only if the form was submitted. another thing was if something was wrong being able to have php give default values that where already submitted if there was a problem. so better than me talking let me show you code.

but let me explain what this form is in the first place. this is a medical history form for dogs. this shows when their last medical test for certain things where done on.
---------form.php-------------------
<?
include ("header.php");
include "../connect.php";
?>
<td class="left_content"><div align="center"><strong>Here are some Use full links</strong></div><br />
<?include ("../leftside.php");?>




</td>
<td class="body_content"><strong><h4 id="title"> Medical Record </h4></strong>
<?php
if ($_POST['submit'] == "Update") { // this checks to see if the submitted form name is Update
$name = $_POST['pName'];
$DHLPP = $_POST["DHLPP"];
$Rabies = $_POST["Rabies"];
$Bordatella = $_POST["Bordatella"];
$heartworm = $_POST["heartworm"];
$worming= $_POST["worming"];
$flea= $_POST["flea"];
$Fixed= $_POST["Fixed"];
$pinHips= $_POST["pinHips"];
$OFA= $_POST["OFA"];
$Eye= $_POST["Eye"];
$wellness= $_POST["wellness"];
$sql = "SELECT * FROM Medical where dogName = '$name'";
$results=mysql_query($sql) or die (mysql_error());
$row= mysql_fetch_array($results);
$numrow = mysql_num_rows($results);
while ($row= mysql_fetch_array($results)){
$id = $row['ID'];
}
if ($numrow == 1) {
$sql = "UPDATE Medical SET dogName = '$name', DHLPP = '$DHLPP', Rabies = '$Rabies',
Bordatella = '$Bordatella', heartworm = '$heartworm', worming = '$worming',
flea = '$flea', Fixed = '$Fixed', pinHips = '$pinHips', OFA = '$OFA', Eye = '$Eye',
wellness = '$wellness'
WHERE dogName = '$name'";
$result = mysql_query($sql) or die (mysql_error());
if ($result) { echo "updated"; }
$date = date('Y-m-d');
$sql="INSERT INTO MedicalHistory (dogName, DHLPP, Rabies, Bordatella, heartworm, worming, flea, Fixed, pinHips, OFA, Eye, wellness, dateadd)
VALUES ('$name','$DHLPP','$Rabies','$Bordatella','$heartworm','$worming','$flea','$Fixed','$pinHips','$OFA','$Eye','$wellness','$date')";
$result = mysql_query($sql) or die (mysql_error());
} else {
$name = $_POST['pName'];
$DHLPP = $_POST["DHLPP"];
$Rabies = $_POST["Rabies"];
$Bordatella = $_POST["Bordatella"];
$heartworm = $_POST["heartworm"];
$worming= $_POST["worming"];
$flea= $_POST["flea"];
$Fixed= $_POST["Fixed"];
$pinHips= $_POST["pinHips"];
$OFA= $_POST["OFA"];
$Eye= $_POST["Eye"];
$wellness= $_POST["wellness"];
$sql="INSERT INTO Medical (dogName, DHLPP, Rabies, Bordatella, heartworm, worming, flea, Fixed, pinHips, OFA, Eye, wellness)
VALUES ('$name','$DHLPP','$Rabies','$Bordatella','$heartworm','$worming','$flea','$Fixed','$pinHips','$OFA','$Eye','$wellness')";
$result = mysql_query($sql) or die (mysql_error());
if ($result) { echo "inserted"; }

$date = date('Y-m-d');
$sql="INSERT INTO MedicalHistory (dogName, DHLPP, Rabies, Bordatella, heartworm, worming, flea, Fixed, pinHips, OFA, Eye, wellness, dateadd)
VALUES ('$name','$DHLPP','$Rabies','$Bordatella','$heartworm','$worming','$flea','$Fixed','$pinHips','$OFA','$Eye','$wellness','$date')";
$result = mysql_query($sql) or die (mysql_error());

}

} else {if ($_POST['submit'] == "Delete Dog") {
$name = $_POST['pName'];

$sql = "DELETE FROM Medical WHERE dogName = '$name'";
$result = mysql_query($sql) or die (mysql_error());
if ($result) {echo "Deleted Dog";}
}
}
$name = $_POST['pName'];
$sql = "SELECT * FROM Medical where dogName = '$name'";
$results=mysql_query($sql) or die (mysql_error());

$row= mysql_fetch_array($results);

$DHLPP = $row["DHLPP"];
$Rabies = $row["Rabies"];
$Bordatella = $row["Bordatella"];
$heartworm = $row["heartworm"];
$worming= $row["worming"];
$flea= $row["flea"];
$Fixed= $row["Fixed"];
$pinHips= $row["pinHips"];
$OFA= $row["OFA"];
$Eye= $row["Eye"];
$wellness= $row["wellness"];

?>
<form method="POST" action="">
<?php
$query = "SELECT * FROM Dogs";
$arr = mysql_query($query);


echo "<select name='pName'>";
echo "<option value=" . $name . ">" . $name . "</option>";
while ($row = mysql_fetch_array($arr)) {
$title = $row["Name"];

if ($title != $name) {
echo "<option value=" . $title . ">" . $title . "</option>";
}
}
echo "</select>";
?>
<br />
<input type="submit" value="Change Dogs">
</form>

<form method="POST" action="">
<table>
<tr>
<td colspan="2">
<input type="hidden" name="pName" value="<?php print $name; ?>"/>

</td>
</tr>
<tr>
<td>DHLPP</td>
<td><input type="date" name="DHLPP" value="<?php if ($DHLPP != "0000-00-00"){print $DHLPP; } ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>Rabies</td>
<td><input type="date" name="Rabies" value="<?php if ($Rabies != "0000-00-00"){print $Rabies;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>Bordatella</td>
<td><input type="date" name="Bordatella" value="<?php if ($Bordatella != "0000-00-00"){ print $Bordatella;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>heartworm</td>
<td><input type="date" name="heartworm" value="<?php if ($heartworm != "0000-00-00"){ print $heartworm;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>worming</td>
<td><input type="date" name="worming" value="<?php if ($worming != "0000-00-00"){ print $worming;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>flea</td>
<td><input type="date" name="flea" value="<?php if ($flea != "0000-00-00"){ print $flea;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>Fixed</td>
<td><input type="date" name ="Fixed" value="<?php if ($Fixed != "0000-00-00"){ print $Fixed; } ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>pinHips</td>
<td><input type="date" name="pinHips" value="<?php if ($pinHips != "0000-00-00"){ print $pinHips;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>OFA</td>
<td><input type="date" name="OFA" value="<?php if ($OFA != "0000-00-00"){ print $OFA;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>Eye</td>
<td><input type="date" name="Eye" value="<?php if ($Eye != "0000-00-00"){ print $Eye; } ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td>Wellness</td>
<td><input type="date" name="wellness" value="<?php if ($wellness != "0000-00-00"){ print $wellness;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Update" />
<input type="submit" name="submit" value="Delete Dog" />
</td>
</tr>
</table>
</form>




<?php

$ip = $_SERVER['REMOTE_ADDR'];
$username = $_COOKIE['Username'];
$date = date('Y-m-d H:i:s');
$sql = "insert into loginHistory (ip, username, datetime) VALUES ('$ip','$username','$date')";
$result=mysql_query($sql);
?>

</td>
<?
include ("../footer.php");
?>
---------------------------------


let me explain this in more detail

when a form is either submitted or not it will run this
<tr>
<td>Wellness</td>
<td><input type="date" name="wellness" value="<?php if ($wellness != "0000-00-00"){ print $wellness;} ?>" />(Ex: YYYY-MM-DD)</td>
</tr>
this is date input type which is a standard coming in html5 that makes sure that before someone submits a form it makes sure that it is a date. though if the browser is not html5 compatible then it will treat it like a text input.
the php in this is checking if the variable $wellness does not not equal 0000-00-00 which is a default input in a mysql database for a date type if the input is blank.

so basicly if it equals anything but 0000-00-00 it will print the variable $wellness in the value. so if $wellness = 2010-01-01 then the input in html will look more like
      <td><input type="date" name="wellness" value="2010-01-01" />(Ex: YYYY-MM-DD)</td>

if your wanting to check each input then you can with in the
if ($_POST['submit'] == "Update") {
you can have separate if statements for each input

so if i wanted to check if the date was exactly 10 characters long then i would run a if statment like this

if (strlen($wellness) != 10 ) {
throw some kind of error
}

the possibilities are endless this is just one thing i have done when i was trying to make my input form more dynamic and not having to have multiple pages.

also if your using a GET statement you can do the same thing. the key is that the input needs to have a name of submit and the value can be checked that way.

i hope you enjoyed this little php and html from local tech repair
if you have comments or other ideas i would love to hear them.
thanks for reading,
josh (Local Tech Repair)