Jump to content

recieving POST vars that may or may not exist or have a value


darryldoo

Recommended Posts

Hi everyone, thanks for viewing my post :)

 

I have only just begun (about 1 month) learning PHP so my apologies in advance if my code is a bit dodgy. After building my very first "Hello World" i decided to be a bit more adventerous and design an imaginary booking system for a guest house (the one i dream of owning, lol).

 

I have infact learned so much while doing this and are only just beginning to appreciate the power of PHP, anyway i will get to the point as im aware im starting to sound like a PHP sales rep, lol...

 

My problem..

 

I have included this segment of code which generates suggested available dates to a user with a radio button next to each date so that the user can select the dates required, the available dates are coming form a Mysql query. I think that this part is now working fine.

 

 

 

$checkboxnumber = 0;

echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"enterdetails.php\">";

echo "<input name=\"manualselect\" type=\"hidden\" id=\"manualselect\" value=\"4455896681578924\">";

while ($fetched = mysql_fetch_row($result)) {

foreach ($fetched as $row) {

echo "<input type=\"radio\" name=\"selecteddate$checkboxnumber\" value=".$row." />".$row."<br />";

}

$checkboxnumber ++ ;

}

echo "<br />";

echo "<label> <input type=\"submit\" name=\"button\" id=\"button\" value=\"Book selected dates\" /></label></form>";

 

after the user selects the required dates by checking the appropriate radio buttons, i would like to pass these selected dates over to the next page, however here lies my problem...

 

HTML Source generated by the php script.

 

source shown is from a date search for the dates 2008-09-06 to 2008-09-24 where a booking already exists in the database for the dates 2008-09-10 to 2008-09-24.

 

<div class="results">

<p>some dates available, here they are<br />

<form id="form1" name="form1" method="post" action="enterdetails.php"><input name="manualselect" type="hidden" id="manualselect" value="4455896681578924">

<input type="radio" name="selecteddate0" value=2008-09-06 />2008-09-06<br />

<input type="radio" name="selecteddate1" value=2008-09-07 />2008-09-07<br />

<input type="radio" name="selecteddate2" value=2008-09-08 />2008-09-08<br />

<input type="radio" name="selecteddate3" value=2008-09-09 />2008-09-09<br />

<input type="radio" name="selecteddate4" value=2008-09-21 />2008-09-21<br />

<input type="radio" name="selecteddate5" value=2008-09-22 />2008-09-22<br />

<input type="radio" name="selecteddate6" value=2008-09-23 />2008-09-23<br />

<input type="radio" name="selecteddate7" value=2008-09-24 />2008-09-24<br />

<label> <input type="submit" name="button" id="button" value="Book selected dates" /></label></form></p>

</div>

 

On my recieving page, i honestly have no idea how to recover the POST variables, as due to the nature of the result (different ammounts of dates can be returned). I did think that it may have been possible, on the recieving page, to loop through all possible post variables and test to see if they exist and then if so, assign them to respective variables however... Well in a nutshell, im stuck ???

 

Any help on this would be greatly appreciated, thank you in advance.

 

On a seperate(ish) note,

 

As i previously mentioned i have only just begun to learn PHP and so would be greatful if anyone could give me any pointers/feedback on my date search php script, so far It does seem to work fine however after looking at my code and comparing to other peoples work it seems a little... well... crap, is the only word that comes to mind, lol

 

here it is and pardon the excessive commenting in the code.

 

Ok, first we have two dates posted via a date search form, all with seperate boxes arrival day, month and year.

 

2 Dates are entered requested arrival and requested departure.

 

 

the relevant table in the database is this

tableqs0.th.jpg

 

 

 

 

 

before html head tag we have

 

<?php

 

require_once 'Connections/bookingsystem.php';

 

// set check variable to fail as its safer to fail than pass

$forminputcheck = "fail";

 

if(checkdate($_POST['arrmonth'],$_POST['arrday'],$_POST['arryear']) && checkdate($_POST['depmonth'],$_POST['depday'],$_POST['depyear'])){

 

$forminputcheck = "pass";

 

// get required dates from post variables

 

$arriveday = $_POST['arrday'];

$arrivemonth = $_POST['arrmonth'];

$arriveyear = $_POST['arryear'];

 

$departday = $_POST['depday'];

$departmonth = $_POST['depmonth'];

$departyear = $_POST['depyear'];

 

// convert date into Mysql format

 

$reqarrdate = "$arriveyear-$arrivemonth-$arriveday";

$reqdepdate = "$departyear-$departmonth-$departday";

 

 

 

// calculate difference between 2 dates

 

$calculateddays = round ((strtotime("$reqdepdate") - strtotime("$reqarrdate")) / (60 * 60 * 24));

 

// select database

 

mysql_select_db($database_bookingsystem, $bookingsystem);

 

// query the database

$query = "SELECT date FROM reservation WHERE booked = FALSE AND (date >= '$reqarrdate' AND date <'$reqdepdate')";

$result = mysql_query($query) or die(mysql_error());

 

 

// how many rows did our query return

 

$num_rows = mysql_num_rows($result);

 

 

 

 

}else { $forminputcheck = "fail";

 

}

 

?>

 

 

 

In the Body Of the page we have

 

 

 

<?php // check number of rows against calculated days and make a decision on what to do (0 available, some available, all available).

if ($forminputcheck == "pass"){

if (($num_rows == $calculateddays) && ($num_rows != 0)) {

// all available,

print "<h1> Excellent news, the period from $reqarrdate through to $reqdepdate is available.</h1><br />";

print "Would you like to book this now?";

}elseif ($num_rows >0 && $num_rows< $calculateddays) {

print "some available, here they are";

echo "<br />";

$checkboxnumber = 0;

echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"enterdetails.php\">";

echo "<input name=\"manualselect\" type=\"hidden\" id=\"manualselect\" value=\"4455896681578924\">";

while ($fetched = mysql_fetch_row($result)) {

foreach ($fetched as $row) {

echo "<input type=\"radio\" name=\"selecteddate$checkboxnumber\" value=".$row." />".$row."<br />";

}

$checkboxnumber ++ ;

}

echo "<br />";

echo "<label> <input type=\"submit\" name=\"button\" id=\"button\" value=\"Book selected dates\" /></label></form>";

}else{

print "Unfortunatley the dates are booked";

}

}else{

print "please input correct dates";

// i was going to implement 3 strikes and then you get banned for a set period of time to prevent any malicious attempts however i havent got a clue how to do this at present, lol

$strike = 1;

 

}

 

 

?>

 

 

some of my html tags for line breaks seem to have been interpereted as actual line breaks as i posted this so they are missing my apologies as i dont know how to stop this happening

 

 

I know its going to be crap, however as its only for learning purposes and considering ive only just got started with PHP, im bloody proud of it, lol...

 

Any comments or feeback on how it could be improved (i bet someone else could write the code to do this in one line, lol) or errors would be more than welcome. If/When i ever get it all finished i will post the link so anyone who is interested can have a play with it...

 

Have a good day all

 

Jacob:)

Archived

This topic is now archived and is closed to further replies.

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