Jump to content

[SOLVED] Problem with checking require fields.


ubuntu

Recommended Posts

Dear all, can someone help me what's wrong with my code? The problem is when I send a blank (without select any field) page, no error was display. Thank you for your kind attention.

 

Form for user to input

table cellpadding="0" cellspacing="0" border="0" width="94%" align="center">
			<tr>
				<td valign="top"><b>From</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="from" class="field">
				<option value="-">---------- Location ----------</option>

				<option value="66">Kuala Lumpur</option>
				<option value="67">Batu Pahat</option>
				<option value="68">Muar</option>
				</select></td>

			</tr>

			<tr>
				<td valign="top"><b>To</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="to" class="field">
				<option value="-">---------- Location ----------</option>

				<option value="66">Kuala Lumpur</option>
				<option value="67">Batu Pahat</option>
				<option value="68">Muar</option>
				</select></td>
			</tr>

		    <tr>
				<td valign="top"><b>Month</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="month" class="field">
				<option value="-">-----------------</option>
				<option value="jan">January</option>
				<option value="feb">February</option>
				<option value="mar">March</option>
				<option value="apr">April</option>
				<option value="may">May</option>
				<option value="jun">June</option>
				<option value="jul">July</option>
				<option value="aug">August</option>
				<option value="sep">September</option>
				<option value="oct">October</option>
				<option value="nov">November</option>
				<option value="dec">December</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Year</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="year" class="field">
				<option value="-">-----------------</option>
				<option value="2007">2007</option>
				<option value="2008">2008</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Day</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="day" class="field">
				<option value="-">-----------------</option>
				<option value="mon">Monday</option>
				<option value="tue">Tuesday</option>
				<option value="wed">Wednesday</option>
				<option value="thu">Thursday</option>
				<option value="fri">Friday</option>
				<option value="sat">Saturday</option>
				<option value="sun">Sunday</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Date</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="day_of_week" class="field">
				<option value="-">-----------------</option>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
				<option value="6">6</option>
				<option value="7">7</option>
				<option value="8">8</option>
				<option value="9">9</option>
				<option value="10">10</option>
				<option value="11">11</option>
				<option value="12">12</option>
				<option value="13">13</option>
				<option value="14">14</option>
				<option value="15">15</option>
				<option value="16">16</option>
				<option value="17">17</option>
				<option value="18">18</option>
				<option value="19">19</option>
				<option value="20">20</option>
				<option value="21">21</option>
				<option value="22">22</option>
				<option value="23">23</option>
				<option value="24">24</option>
				<option value="25">25</option>
				<option value="26">26</option>
				<option value="27">27</option>
				<option value="28">28</option>
				<option value="29">29</option>
				<option value="30">30</option>
				<option value="31">31</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Time</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="time" class="field">
				<option value="-">-----------------</option>
				<option value="twelve">12:00pm</option>
				<option value="third">03:00pm</option>
				<option value="fifth">05:00pm</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Quantity</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="quantity" class="field">
				<option value="-">-----------------</option>
				<option value="one">1</option>
				<option value="two">2</option>
				</select></td>

			</tr>

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

 

Code to check require field

<?php
$error = NULL;

if($_POST['from'] == "-"){
    $error .= "Error, you haven't select from field.<br>";
}

if($_POST['to'] == "-"){
    $error .= "Error, you haven't select to field.<br>";
}

if($_POST['month'] == "-"){
    $error .= "Error, you didn't select month.<br>";
}

if($_POST['year'] == "-"){
    $error .= "Error, you didn't select year.<br>";
}

if($_POST['day'] == "-"){
    $error .= "Error, you didn't select day.<br>";
}

if($_POST['day_of_week']  == "-"){
    $error .= "Error, you didn't select date.<br>";
}

if($_POST['time'] == "-"){
    $error .= "Error, you didn't select departure time.<br>";
}

if($_POST['quantity'] == "-"){
    $error .= "Error, you didn't select quantity.<br>";
}

if (!is_null($error)) {
    die($error);
}
?>

Link to comment
Share on other sites

Change your code with stuff like this:

 

<?php
$error = NULL;

if($_POST['from'] == "-"){
    die("This is working so far!");
    $error .= "Error, you haven't select from field.<br>";
}

if($_POST['to'] == "-"){
    die("This is working so far!");
    $error .= "Error, you haven't select to field.<br>";
}

if($_POST['month'] == "-"){
    die("This is working so far!");
    $error .= "Error, you didn't select month.<br>";
}

if($_POST['year'] == "-"){
    die("This is working so far!");
    $error .= "Error, you didn't select year.<br>";
}

if($_POST['day'] == "-"){
    die("This is working so far!");
    $error .= "Error, you didn't select day.<br>";
}

if($_POST['day_of_week']  == "-"){
    die("This is working so far!");
    $error .= "Error, you didn't select date.<br>";
}

if($_POST['time'] == "-"){
    die("This is working so far!");
    $error .= "Error, you didn't select departure time.<br>";
}

if($_POST['quantity'] == "-"){
    die("This is working so far!");
    $error .= "Error, you didn't select quantity.<br>";
}

if (!is_null($error)) {
    die($error);
}
?>

 

Tell me what happens.

Link to comment
Share on other sites

Aright so its not even picking up the post variables. Two more things:

 

1. I see in your html you lack a <form action="page.php" method="post"> </form> element? Could that be it?

2. If you have one do this at the very top do this:

 

$var = $_POST['from'];
die("+$var+");

 

That will tell you what is being picked up if anything.

Link to comment
Share on other sites

You sure? I just tested this code on my server:

 

<?php
if($_POST['submit']){
$test = $_POST['from'];
die("+$test+");
}
?>
<form action="v.php" method="post">
<table cellpadding="0" cellspacing="0" border="0" width="94%" align="center">
			<tr>
				<td valign="top"><b>From</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="from" class="field">
				<option value="-">---------- Location ----------</option>

				<option value="66">Kuala Lumpur</option>
				<option value="67">Batu Pahat</option>
				<option value="68">Muar</option>
				</select></td>

			</tr>

			<tr>
				<td valign="top"><b>To</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="to" class="field">
				<option value="-">---------- Location ----------</option>

				<option value="66">Kuala Lumpur</option>
				<option value="67">Batu Pahat</option>
				<option value="68">Muar</option>
				</select></td>
			</tr>

		    <tr>
				<td valign="top"><b>Month</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="month" class="field">
				<option value="-">-----------------</option>
				<option value="jan">January</option>
				<option value="feb">February</option>
				<option value="mar">March</option>
				<option value="apr">April</option>
				<option value="may">May</option>
				<option value="jun">June</option>
				<option value="jul">July</option>
				<option value="aug">August</option>
				<option value="sep">September</option>
				<option value="oct">October</option>
				<option value="nov">November</option>
				<option value="dec">December</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Year</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="year" class="field">
				<option value="-">-----------------</option>
				<option value="2007">2007</option>
				<option value="2008">2008</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Day</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="day" class="field">
				<option value="-">-----------------</option>
				<option value="mon">Monday</option>
				<option value="tue">Tuesday</option>
				<option value="wed">Wednesday</option>
				<option value="thu">Thursday</option>
				<option value="fri">Friday</option>
				<option value="sat">Saturday</option>
				<option value="sun">Sunday</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Date</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="day_of_week" class="field">
				<option value="-">-----------------</option>
				<option value="1">1</option>
				<option value="2">2</option>
				<option value="3">3</option>
				<option value="4">4</option>
				<option value="5">5</option>
				<option value="6">6</option>
				<option value="7">7</option>
				<option value="8">8</option>
				<option value="9">9</option>
				<option value="10">10</option>
				<option value="11">11</option>
				<option value="12">12</option>
				<option value="13">13</option>
				<option value="14">14</option>
				<option value="15">15</option>
				<option value="16">16</option>
				<option value="17">17</option>
				<option value="18">18</option>
				<option value="19">19</option>
				<option value="20">20</option>
				<option value="21">21</option>
				<option value="22">22</option>
				<option value="23">23</option>
				<option value="24">24</option>
				<option value="25">25</option>
				<option value="26">26</option>
				<option value="27">27</option>
				<option value="28">28</option>
				<option value="29">29</option>
				<option value="30">30</option>
				<option value="31">31</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Time</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="time" class="field">
				<option value="-">-----------------</option>
				<option value="twelve">12:00pm</option>
				<option value="third">03:00pm</option>
				<option value="fifth">05:00pm</option>
				</select></td>

			</tr>

		    <tr>
				<td valign="top"><b>Quantity</b></td>
				<td valign="top"><b> : </b></td>
				<td valign="top"><select name="quantity" class="field">
				<option value="-">-----------------</option>
				<option value="one">1</option>
				<option value="two">2</option>
				</select></td>

			</tr>

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

 

And it displayed fine.

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.