-1

I have an HTML file with a check box as follows:

 <body>
<div id="wb_Web_Reports" style="position:absolute;left:329px;top:143px;width:510px;height:244px;z-index:4;">
<form name="Web_Reports" method="post" action="stsarrival.php" enctype="text/plain" id="Web_Reports">
<input type="submit" id="Button1" name="" value="Submit" style="position:absolute;left:210px;top:184px;width:96px;height:25px;z-index:0;">
<input type="text" id="ArrivalDate" style="position:absolute;left:140px;top:71px;width:253px;height:33px;line-height:33px;z-index:1;" name="ArrivalDate" value="">
<div id="wb_Text1" style="position:absolute;left:195px;top:34px;width:234px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">From Arrival Date:</span></div>
<input type="checkbox" id="Checkbox1" name="Checkbox1" value="on" style="position:absolute;left:442px;top:71px;z-index:3;" onclick="document.getElementById('ArrivalDate').disabled=this.checked;">
   </form>

The php file to which this is posting (stsarrival.php) should run a query if the check box is selected and run a different query if the check box is not selected. I wrote the following:

if (isset($_POST['[Checkbox1]'])) {

    $result = mysql_query("SELECT *  FROM Main_Data_Table INNER JOIN Title ON 
Main_Data_Table.TitleID = Title.ID INNER JOIN Agent ON Main_Data_Table.AgentID = Agent.ID 
INNER JOIN Accommodation ON Main_Data_Table.AccommodationID = Accommodation.ID 
INNER JOIN `Host Family details` ON Main_Data_Table.HostFamilyID = `Host Family details`.ID 
INNER JOIN Transfers ON Main_Data_Table.TransferId = Transfers.ID 
INNER JOIN `Board Basis` ON Main_Data_Table.BoardBasisID = `Board Basis`.ID 
INNER JOIN `Course Booked` ON Main_Data_Table.CourseTypeID = `Course Booked`.ID");
} else {

    $result = mysql_query("SELECT *  FROM Main_Data_Table INNER JOIN Title ON 
Main_Data_Table.TitleID = Title.ID INNER JOIN Agent ON Main_Data_Table.AgentID = Agent.ID 
INNER JOIN Accommodation ON Main_Data_Table.AccommodationID = Accommodation.ID 
INNER JOIN `Host Family details` ON Main_Data_Table.HostFamilyID = `Host Family details`.ID 
INNER JOIN Transfers ON Main_Data_Table.TransferId = Transfers.ID 
INNER JOIN `Board Basis` ON Main_Data_Table.BoardBasisID = `Board Basis`.ID 
INNER JOIN `Course Booked` ON Main_Data_Table.CourseTypeID = `Course Booked`.ID 
  WHERE (Main_Data_Table.`Arrival Date` = '2014-09-01')");
}

However I am always getting the result of one query regardless of whether the check box is selected or not.

Please help...

thanks

elstiv
  • 383
  • 5
  • 27

6 Answers6

1

isset returns a boolean, so it will never equal the string "on". Simply remove that:

if (isset($_POST['Checkbox1'])) {

There is no need to check the value, as the key will not exist in the post array if the checkbox is not selected

Steve
  • 20,703
  • 5
  • 41
  • 67
  • Then something else is wrong, as this code is as simple as can be. What does `var_dump($_POST);` look like? – Steve Sep 02 '14 at 12:46
  • it returns array(0) { } – elstiv Sep 02 '14 at 12:49
  • @elstiv OK then none of your form data is being posted. Please edit your question to show the complete form – Steve Sep 02 '14 at 13:20
  • @elstiv you need to remove `enctype="text/plain"` from your form tag, then everything should work – Steve Sep 02 '14 at 13:47
  • For an explanation why: http://stackoverflow.com/questions/7628249/method-post-enctype-text-plain-are-not-compatible – Steve Sep 02 '14 at 13:50
  • my var_dump is now showing: array(2) { ["ArrivalDate"]=> string(0) "" ["Checkbox1"]=> string(1) "1" } – elstiv Sep 02 '14 at 14:06
1

Ok the solution was very very simple - I am ashamed to say...

I was writing

['[Checkbox1]'] 

instead of

 ['Checkbox1']

so the full working line is:

if (isset($_POST['Checkbox1'])&& $_POST['Checkbox1'] == 1)

there were two extra [ ]...

Thanks everyone

elstiv
  • 383
  • 5
  • 27
1

Something like this...

$query  = "
SELECT *  
  FROM Main_Data_Table m
  JOIN Title ti
    ON m.TitleID = ti.ID 
  JOIN Agent ag
    ON m.AgentID = ag.ID 
  JOIN Accommodation ac
    ON m.AccommodationID = ac.ID 
  JOIN `Host Family details` d
    ON m.HostFamilyID = d.ID 
  JOIN Transfers tr
    ON m.TransferId = tr.ID 
  JOIN `Board Basis` b
    ON m.BoardBasisID = b.ID 
  JOIN `Course Booked` c
    ON m.CourseTypeID = c.ID
";


if (isset($_POST['Checkbox1'])&& $_POST['Checkbox1'] == 1){
$query .=  " WHERE m.`Arrival Date` = '2014-09-01'";
}

... but don't use "SELECT *" and don't use spaces in column/table names. IT WILL DRIVE YOU BONKERS

Strawberry
  • 33,750
  • 13
  • 40
  • 57
0

Change code from:

if (isset($_POST['Checkbox1'])== "on") {

To:

if (isset($_POST['Checkbox1']) && $_POST['Checkbox1'] == "on") {

Also try not to use the mysql_* functions, as they are deprecated!

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Change the code like this:

if (isset($_POST['Checkbox1']) && $_POST['Checkbox1'] == "on") 
{
}
else 
{
}
Stonz2
  • 6,306
  • 4
  • 44
  • 64
teshvenk
  • 403
  • 3
  • 8
0

Do not give the value of the check box as on or off. Just give a one or zero. Try to submit your form and check for what value comes in through the post. Based on the value you can write the conditions you want.

Vivek Cu
  • 127
  • 1
  • 2
  • 9