Forum Moderators: coopster

Message Too Old, No Replies

if statement in php

         

scoobie

3:12 pm on Apr 13, 2005 (gmt 0)

10+ Year Member



Hi,

i have a form where the user is given two menus each with four options the user can choose from.

Code
<input type = "radio" name = TimeDataFrm value = "Data" checked>Data
<select name = DataOptionFrm>
<?php
$qry = "SELECT TimeData, Cost, Description FROM Chrgtbl WHERE TimeData Like '%MB%'";

$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
echo "<option value = '".$row['TimeData']."'>".$row['TimeData']." ¦¦ € ".$row['Cost']." ¦¦ ".$row['Description']."</option>";
?>
</select>

<input type = "radio" name = TimeDataFrm value = "Data" checked>Time
<select name = TimeOptionFrm>
<?php
$qry = "SELECT TimeData, Cost, Description FROM Chrgtbl WHERE TimeData Like '%MB%'";

$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
echo "<option value = '".$row['TimeData']."'>".$row['TimeData']." ¦¦ € ".$row['Cost']." ¦¦ ".$row['Description']."</option>";
?>
</select>

when the user selects from one of the menus and clicks enter it runs a insert.php script that executes an insert query. if the user chooses from the data menu i want it to input the data info in the mysql db table else if they select from the time menu i want it to input the time info into the table.

i have used the following if but can't get it to work:

if ($DataOptionFrm)
{
$qry = "select ChrgID from chrgtbl where chrgtbl.TimeData = '$DataOptionFrm'";
$result = mysql_query($qry) or die(mysql_error());
$row = mysql_fetch_object($result);
$insert = "insert into tickettbl (AccID, ChrgID, TicketNo, TimeData, RecDate)
values ('$TTAccID', '$row->ChrgID', '$Random', '$DataOptionFrm', '$TodaysDate')";

$result = mysql_query($insert) or die(mysql_error());
}

else
{
$qry = "select ChrgID from chrgtbl where chrgtbl.TimeData = '$TimeOptionFrm'";
$result = mysql_query($qry) or die(mysql_error());
$row = mysql_fetch_object($result);
$insert = "insert into tickettbl (AccID, ChrgID, TicketNo, TimeData, RecDate)
values ('$TTAccID', '$row->ChrgID', '$Random', '$TimeOptionFrm', '$TodaysDate')";
$result = mysql_query($insert) or die(mysql_error());
}
if ($result)
echo "Details Entered\n\n";

it just keeps entering the 1st data menu option even if i choose from the time menu

Can anyone help, i'm really stuck.

Thanks,

scoobie

Timotheos

6:07 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi scoobie,

So you have just one form tag I'm assuming which would mean that $DataOptionFrm will always have a value and thus be true. Probably the easiest thing to do is to use a form for each menu and that way $DataOptionFrm won't get set.

<form name="a" action="whereever.php">
<input type = "radio" name = TimeDataFrm value = "Data" checked>Data
<select name = DataOptionFrm>
blah blah
</select>
</form>

<form name="b" action="whereever.php">
<input type = "radio" name = TimeDataFrm value = "Data" checked>Time
<select name = TimeOptionFrm>
blah blah
</select>
</form>

Tim

mcibor

8:17 pm on Apr 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that the main problem is with the if ($DataOptionFrm) statement. First of all you never define $DataOptionFrm, and second if it's "0" or "false" then the statement is false.

This is how to do it:

if(isset($_POST["DataOptionFrm"]))

And moreover this should be written in <form>:

<form name="form" action="index.php" method="POST">

The way Timotheos suggestes isn't the best one (you have two distinguish names of the selects).

If you wish you can also add auto form submiting:

<option ...... onclick="document.form.submit();">...

Best regards
Michal Cibor