0

I want to access variable on same page but I can't .. my code is given below but the errors occurs Undefined index: cars

<form name="form1" action="#" method="post">
<select onChange="openOffersDialog(this.value);" name="cars" id="cars">  
  <option value="Volvo" id="Volvo">Volvo</option>        
  <option id="Saab" value="Saab" selected>Audi</option>                  
</select>
</form>
<?php $car = $_GET['cars'];
    switch ($car) { 
    case "Volvo": 
    echo  "<table width='50%' border='0'>
          <tr>
            <td>Volvo1</td>
            <td>Volvo2</td>
          </tr>      
        </table>"; 
    break; 
    case "Saab": 
    echo  "<table width='50%' border='0'>
          <tr>
            <td>Saab1</td>
            <td>Saab2</td>
          </tr>      
        </table>" ;
    break;
    default: 
    echo "Sorry,<br>";  
    } ?>
  • You're using POST instead of GET in the method. Also always check if a variable exists especially post/get : `$car = isset($_GET['cars']) ? $_GET['cars']: '';` – HamZa May 27 '13 at 09:44
  • Why the downvotes? Problem was trivial, but legitimate. I know it’s Monday, but cheer up. – Jezen Thomas May 27 '13 at 09:51
  • @JezenThomas That moment when such trivial questions get downvoted but the answers get upvoted, I feel sorry for the community ... – HamZa May 27 '13 at 09:53
  • @HamZa DzCyberDeV I just tried as you suggested and also correct my mistake at
    instead of post I set "get" ... still problem in unsolved ....!!
    – Shilpa Deshmukh May 27 '13 at 10:26
  • @ShilpaDeshmukh "problem unsolved" ? What does that mean ? Do you get an error ? empty page ? or "Sorry" ? – HamZa May 27 '13 at 10:28
  • @HamZa DzCyberDeV Undefined index: cars – Shilpa Deshmukh May 27 '13 at 10:35
  • @ShilpaDeshmukh sorry but that's just impossible. Replace `$car = $_GET['cars'];` with my code above and make sure you're saving it and not loading from cache. – HamZa May 27 '13 at 10:37
  • @HamZa DzCyberDeV I have done same change as you suggested and I am sure that I saved my php page before executing ...and while refreshing page on browser I used ctrl+f5........... can I send you whole code because rest code is running properly just showing error here only – Shilpa Deshmukh May 27 '13 at 10:48
  • @ShilpaDeshmukh Ok, [try this](http://codepad.org/UEDA6bx8), I've tested it and it works. – HamZa May 27 '13 at 10:52
  • Thank you very much to help me I have send you code by e-mail – Shilpa Deshmukh May 27 '13 at 10:59
  • @ShilpaDeshmukh I'll maybe check it later ... – HamZa May 27 '13 at 11:59

3 Answers3

2

You have three problems:

  1. Your form is method="post" but you are trying to read data out of $_GET, change that to $_POST (or change the form to use method="get", see this answer for help determining which you should use).
  2. You are trying to read data out of the form submission whether or not the form has actually been submitted. Test if the value exists with isset before performing the rest of your logic that depends on it.
  3. You have no submit button, so the form cannot be submitted. You might be expecting the PHP to run when the select is changed, but unless you have some JavaScript to trigger the form submission, that won't be the case.
Community
  • 1
  • 1
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

You have a POST method in your form. Replace $_GET["cars"] with $_POST["cars"].

$car = $_POST['cars'];

Or, you need to change in your <form> tag:

<form name="form1" action="#" method="get">

And moreover, you should first check if the form has been posted. So you need to give this upfront.

<?php if (count($_POST) && isset($_POST["cars"])) { ?>

And then you need to proceed.

Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226
0

Where you have gone wrong is $_GET. Try $_POST['cars']; method. Please read the link below for more information

http://www.tutorialspoint.com/php/php_get_post.htm

Techie
  • 42,101
  • 38
  • 144
  • 232