Jump to content

Why doesn't my variable have a value?


gigabyt3r

Recommended Posts

When I select a checkbox, no results are returned so I echoed the variable to the page and its empty which I cant seem to understand why?

 

Here is the PHP code

use this here ->

[code]
//display all roof height vehicles when submit button isnt pressed
if (!isset($_POST['Submit'])) {
$RoofHeight = "N/A', 'Standard Roof', 'Medium Roof', 'High Roof";
}


//N/A Roof Height Only
if (isset($_POST['Submit']) && isset($_POST['N/A'])) {
$RoofHeight = $RoofHeight."N/A";
}

//Standard Roof Only
if (isset($_POST['Submit']) && isset($_POST['Standard Roof'])) {
// add comma if necessary to separate

if (!empty($RoofHeight))
{
$RoofHeight = $RoofHeight."', '";
}
$RoofHeight = $RoofHeight."Standard Roof";
}

//Medium Roof Only
if (isset($_POST['Submit']) && isset($_POST['Medium Roof'])) {
// add comma if necessary to separate

if (!empty($RoofHeight))
{
$RoofHeight = $RoofHeight."', '";
}
$RoofHeight = $RoofHeight."Medium Roof";
}


//High Roof ONLY
if (isset($_POST['Submit']) && isset($_POST['High Roof'])) {
// add comma if necessary to separate

if (!empty($RoofHeight))
{
$RoofHeight = $RoofHeight."', '";
}
$RoofHeight = $RoofHeight."High Roof";
}

Thanks in advance

 

and this here -> [/CODE]

in the future

-zanus

Link to comment
Share on other sites

Which variable? Which checkbox? You have not exactly told or shown anyone here what exactly you are having a problem with.

 

You would need to post your form (where the data that is not working is coming from) and state which part of it is not working (so we know where to start looking for the problem.)

Link to comment
Share on other sites

Here is the form;

 

  <p><strong><u>Roof Height</u></strong></p>
  <p>
    <input type="checkbox" name="N/A" value="N/A"  />
    N/A Roof Height Vehicles </p>
  <p>
    <input type="checkbox" name="Standard Roof" value="Standard Roof"  />
    Standard Roof  </p>
  <p>
    <input type="checkbox" name="Medium Roof" value="Medium Roof"  />
  Medium Roof</p>
  <p>
    <input type="checkbox" name="High Roof" value="High Roof"  /> 
    High Roof

 

 

And the PHP code

 

//display all roof height vehicles when submit button isnt pressed
if (!isset($_POST['Submit'])) {
$RoofHeight = "N/A', 'Standard Roof', 'Medium Roof', 'High Roof";
}


//N/A Roof Height Only
if (isset($_POST['Submit']) && isset($_POST['N/A'])) {
q $RoofHeight."N/A";
}

//Standard Roof Only 
if (isset($_POST['Submit']) && isset($_POST['Standard Roof'])) {
$RoofHeight = "Standard Roof";
}

//Medium Roof Only
if (isset($_POST['Submit']) && isset($_POST['Medium Roof'])) {
// add comma if necessary to separate

if (!empty($RoofHeight))
{
$RoofHeight = $RoofHeight."', '";
}
$RoofHeight = $RoofHeight."Medium Roof";
} 


//High Roof ONLY
if (isset($_POST['Submit']) && isset($_POST['High Roof'])) {
// add comma if necessary to separate

if (!empty($RoofHeight))
{
$RoofHeight = $RoofHeight."', '";
}
$RoofHeight = $RoofHeight."High Roof";
} 

 

 

 

 

The variable $RoofHeight does not have a value when I select a checkbox submit the form and echo the variable.

 

If you need anymore information don't hesitate to ask!

 

Thanks in advance

Link to comment
Share on other sites

That's not a complete form, so nothing is submitted anyway. However, if your actual form does submit data, add the following lines of code so that you can see exactly what data is being received -

echo "<pre>";
echo "POST:";
print_r($_POST);
echo "</pre>";

 

You will find that variable names cannot contain spaces and php converts them to underscores _ in the POST field names.

Link to comment
Share on other sites

Thanks for the reply. I can post the whole form although it is rather large. Also, here is the result from the code you posted

 

 

POST:Array

(

    [standard_Roof] => Standard Roof

    [colour] =>

    [textfield2] =>

    [textfield3] =>

    [textfield4] =>

    [submit] => Submit

    [orderBy] => `Reg Year`

)

 

Link to comment
Share on other sites

Get rid of the isset($_POST['submit'])  I'm guessing you thought you neeeded that. But if you dont have actually have a post variable called "submit" set. Am I right?

 

Also remove the spaces from the name="" attributes. Turn "Standard Roof" to "StandardRoof"

 

 

Edit: I see you do have a post var being sent called Submit.. But regardless you need to get rid the space(s) in the Input name="" attribute as PFMaBiSmAd said.

Link to comment
Share on other sites

Also

//N/A Roof Height Only
if (isset($_POST['Submit']) && isset($_POST['N/A'])) {
q $RoofHeight."N/A";
}

 

Is invalid.  What is q? Thats not valid syntax. You might want to check that.

 

Thanks, I removed the spaces in the form name and removed the 'q' don't quite know how it got there! It still isn't working though :(

Link to comment
Share on other sites

I tried runnng your code on my server and it worked. Except for the $_POST['submit']. I removed that and used the form you provided. (after removing the spaces) and it worked. 

 

Can I ask a question. Are you assigning  "Submit" to your submit button thinking that it sends a variable called Submit and trying to check for it? The submit button doesn't do that. Its not necessary.  :D

 

Link to comment
Share on other sites

The original posted code had a correct line of code where the q... issue is at. Something was lost in the copy/paste.

 

When you removed the space from the form field names, you need to make the corresponding change to the $_POST variable names. Computers only do exactly what their code tells them to do and making a change where something is produced requires a corresponding change where that something is used. The code I posted with the print_r($_POST); was for you to see what variables and data your form is sending, making it possible for you to produce code that uses those variables and data.

Link to comment
Share on other sites

After much tweaking and frustration i found that when I select a roof height it doesn't work

$query_rsVehicle = "SELECT ID, `Vehicle Registration`, Make, Model, Color, Transmission, `Fuel Type`, Mileage, `Reg Year`, `Image Prefix`, `Key Point 1`, `Key Point 2`, `Key Point 3`, `Key Point 4` FROM vehicles WHERE Model LIKE \"%$Search%\" AND Wheelbase IN ('$Wheelbase') AND `Roof Height` IN ('$RoofHeight')"; 

 

but when i take the

AND Wheelbase IN ('$Wheelbase')

it works! The problem is I need them both to work!

 

 

Anyone have any idea what the best way around this is?

 

 

Link to comment
Share on other sites

I think its because of

if (!isset($_POST['Submit'])) {
$Wheelbase = "SWB', 'MWB', 'LWB";
}

 

And of course if you select a roof height then the Submit button is posted although there is no Wheelbase value!

 

 

It sounds a little confusing but if you dont understand just say and I will try explain better.

 

Thanks in advance

 

 

 

 

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.