select variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

select variable

Post by gum1982 »

hello need some help.

i need to parse through a select variable from a select drop down box to run an if else statement on the posted page.

<form action="availiable.php" method="post">
<fieldset>
<select>
<option>Choose A Chalet:</option>

<option value="<?php echo "$caswell" ;?></option>
<option value="<?php echo "$oxwich" ;?></option>

</select>
</fieldset>
<input type="submit" value="Check Availiability" />
</form>

i need to catch whether caswell or oxwich was selected on the availiable.php page to run a query on the database.

availiable.php file.

<?php include("conninfo.php");

$caswell=$_POST['caswell'];
$oxwich=$_POST['oxwich'];


$strDB=mysql_connect($strServer,$strUser,$strPwd)or die("Could not open database");
$database=mysql_select_db("$strDatabase",$strDB);

if (caswell); {
$query=("SELECT * FROM bookingdates WHERE chaletname like 'Caswell' AND booked=0");

$result = mysql_query($query) or die("Couldn't execute query");?>

if (oxwich); {
$query=("SELECT * FROM bookingdates WHERE chaletname like 'oxwich' AND booked=0");

$result = mysql_query($query) or die("Couldn't execute query");?>

<table width="439" border="1">
<?
while($r=mysql_fetch_array($result))
{
?>


<tr>
<td width="130"><?php echo $r["chaletname"];?></td>
<td width="61"><?php echo $r["startmonth"];?></td>
<td width="67"><?php echo $r["startday"];?></td>
<td width="71"><?php echo $r["endmonth"];?></td>
<td width="82"><?php echo $r["endday"];?></td>
<td width="67"><?php echo $r["year"];?></td>
<td width="116"><?php echo $r["weekprice"];?></td>
</tr>
<?
}
?>

can someone please help, sorry in advance really new to php.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: select variable

Post by mattpointblank »

You need to structure your HTML like this in order to get $_POST values from it:

Code: Select all

 
<select name="something">
<option>Choose A Chalet:</option>
<option value="<?php echo $caswell; ?>">Something Goes Here</option>
<option value="<?php echo $oxwich; ?>">Something Else Goes Here</option>
</select>
 
// ... then
 
$something = $_POST['something'];
 
// $something will now contain either $caswell or $oxwich
 
Similarly, your if statement syntax is a little off:

Code: Select all

 
if ($caswell != "") { // not sure what your variables are, but this one tests that $caswell isn't blank
$query = "SELECT * FROM bookingdates WHERE chaletname like 'Caswell' AND booked=0";
$result = mysql_query($query) or die("Couldn't execute query");
} // don't forget your closing curly bracket!
 
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

Re: select variable

Post by gum1982 »

thanks for the reply

sorry still cant get it to work

index.php

<form action="availiable.php" method="post">
<p>Select an available date:</p>
<select name="something">
<option>Choose A Chalet:</option>
<option value="<?php echo $caswell; ?>">Caswell</option>
<option value="<?php echo $oxwich; ?>">Oxwich</option>
</select>

<input type="submit" value="Book now">
</form>
<?php
$something = $_POST['something'];

//$something will now contain either $caswell or $oxwich

?>

avialiable.php

<?php include("conninfo.php");


$something = $_POST['something'];

//$something will now contain either $caswell or $oxwich




if ($caswell != "") { // not sure what your variables are, but this one tests that $caswell isn't blank
$query = "SELECT * FROM bookingdates WHERE chaletname like 'caswell' AND booked=0";
$result = mysql_query($query) or die("Couldn't execute query");
} // don't forget your closing curly bracket!
?>

<table width="439" border="1">
<?
while($r=mysql_fetch_array($result))
{
?>


<tr>
<td width="130"><?php echo $r["chaletname"];?></td>
<td width="61"><?php echo $r["startmonth"];?></td>
<td width="67"><?php echo $r["startday"];?></td>
<td width="71"><?php echo $r["endmonth"];?></td>
<td width="82"><?php echo $r["endday"];?></td>
<td width="67"><?php echo $r["year"];?></td>
<td width="116"><?php echo $r["weekprice"];?></td>
</tr>
<?
}
?>
</table>

i keep getting an era saying Undefined variable:

my variables are $oxwich and caswell$ basically just want them to be parsed to availiable.php so that i can run the query to display the availiable dates on the the page for the specific chosen chalets.

thanks
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: select variable

Post by McInfo »

You get an "undefined variable" error when you try to use a variable that does not exist. I'm guessing that $caswell and $oxwich are the undefined variables.

PHP Manual: Variables

Try this:

index.php

Code: Select all

<form method="post" action="available.php">
    <select name="chalet">
        <option value="Caswell">Caswell</option>
        <option value="Oxwich">Oxwich</option>
    </select>
    <input type="submit" value="Submit" />
</form>
available.php

Code: Select all

<?php
// Checks that chalet was posted
if (isset($_POST['chalet'])) {
   
    // Assigns shorter, easier-to-type name by reference
    $chalet =& $_POST['chalet'];
   
    // Checks that the input contains only word characters and spaces
    if (0 == preg_match('/[^\w ]+/i', $chalet)) {
       
        // Builds a query
        $query = "SELECT * FROM `bookingdates` WHERE `chaletname` = '{$chalet}' AND `booked` = 0";
       
        // Does something with the query
        echo $query;
    }
}
?>
Also read:

PHP Manual: SQL Injection

Edit: This post was recovered from search engine cache.
Post Reply