Jump to content

help finding error on form required field

Recommended Posts

i was trying to make error message relating from the w3schools learning center but it returned nothing.

please help me find the error....

here is the code i wrote...

<?php// define variables and set to empty values$cnameErr = $dateErr = $itemErr = $rateErr = $qtyErr = "";$cname = $date = $rfn = $item = $rate = $qty = $less = $amt = "";if ($_SERVER["REQUEST_METHOD"] == "POST"){  if (empty($_POST["cname"]))    {$cnameErr = "Customer Name is required";}  else    {$cname = test_input($_POST["cname"]);}  if (empty($_POST["date"]))    {$emailErr = "Date is required";}  else    {$date = test_input($_POST["date"]);}  if (empty($_POST["rfn"]))    {$rfn = "";}  else    {$rfn = test_input($_POST["rfn"]);}  if (empty($_POST["item"]))    {$itemErr = "Item name is required";}  else    {$item = test_input($_POST["item"]);}  if (empty($_POST["rate"]))    {$rateErr = "Please input Rate";}  else    {$rate = test_input($_POST["rate"]);}  if (empty($_POST["qty"]))    {$qtyErr = "Please input item quantity";}  else    {$qty = test_input($_POST["qty"]);}  if (empty($_POST["less"]))    {$less = "";}  else    {$less = test_input($_POST["less"]);}  if (empty($_POST["amt"]))    {$amt = "";}  else    {$amt = test_input($_POST["amt"]);}}function test_input($data){     $data = trim($data);     $data = stripslashes($data);     $data = htmlspecialchars($data);     return $data;}// error messageecho $dateErr; // and so on?>
Link to post
Share on other sites

thank u


but this returned nothing when i click submit to a blank form.


the form is like this--

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST"><fieldset class="form-field-set"><legend><h1>Test Invoice</h1></legend><div class="form-field" id="main"><div class="form-field"><span class="error">* <? echo $cnameErr; ?></span><label>Customer Name:</label><select name="cname" id="cname"><option></option><?php// select table$query = "SELECT * FROM customer";$result = mysql_query($query);if (!$result) die("Unable to select database: " . mysqli_error());// fetch data$rows = mysql_num_rows($result);for ($n = 0 ; $n < $rows ; ++$n){		echo "<option>" . mysql_result($result,$n,'cname') . "</option>" ;}?></select></div><div class="form-field"><span class="error">* <? echo $dateErr; ?></span><label>Date:</label><input type="date" name="date" id="date"></div><div class="form-field"><label>Reference No.:</label><input type="text" name="rfn" id="rfn"></div><div class="form-field"><span class="error">* <? echo $itemErr; ?></span><label>Item Name:</label><select name="item"><option></option><?php// select table$query = "SELECT * FROM stock";$result = mysql_query($query);if (!$result) die("Unable to select database: " . mysqli_error());// fetch data$rows = mysql_num_rows($result);for ($n = 0 ; $n < $rows ; ++$n){		echo "<option>" . mysql_result($result,$n,'item') . "</option>" ;}?></select></div><div class="form-field"><span class="error">* <? echo $rateErr; ?></span><label>Rate:</label><input type="text" name="rate" id="rate"></div><div class="form-field"><span class="error">* <? echo $qtyErr; ?></span><label>Quantity:</label><input type="text" name="qty" id="qty"></div><div class="form-field"><label>Discount (%):</label><input type="text" name="less" id="less"></div><div class="form-field"><label>Amount:</label><input type="text" name="amt" id="amt" disabled></div><div class="form-field"><input type="submit" value="Add to Cart" name="submit" id="submit"></div></form>
Link to post
Share on other sites

i got what the problem was. before, i used --

<? echo $dateErr; ?>

later i changed it to --

<?php $dateErr; ?>

and it worked. but i dont know what the different is.....

Edited by funbinod
Link to post
Share on other sites

you would still need the echo either way.

i've echoed it and got the result... please recheck on the last line in my first post

// error messageecho $dateErr; // and so on

and in between on several places in my second post......

<span class="error">* <? echo $dateErr; ?></span> //and so on
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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