1

I am working off a snippet of someone else's code I have found. I would like to update a PHP variable once the select field has changed.

The name of the of the variable is $ProductType as below. The current code does what is expected so now all I'd like to do is set the variable equal to the changed option

<?php
  $ProductType = '';

  if(isset($_GET['trade'])){
    //Everything in here will get echoed in the DIV
    echo "You selected: ".$_GET['trade'];
    $ProductType = $_GET['trade']; // I'd have thought this might work but when I echo $ProductType, it returns nothing.
    exit;
  }

?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<form name="x" action="" method="POST">
  <select name="trade" onchange="$('#extra').load('?trade='+this.value);">
    <option value="1">item1</option>
    <option value="2">item2</option>
    <option value="3">item3</option>
    <option value="4">item4</option>
  </select>
  <input type="submit" value="SEARCH" class="submit-button btn" />
</form>

<div id="extra" style="color:red;"></div>

EDIT:

I've added a search button this time. I've echoed it out again and it seems it is working, however, it is returning an array which is not what I am after. So once the page loads, the user will select from the dropdown and the variable $ProductType should change. If I then submit the form, the page loads again and my variable updates my query.

SixfootJames
  • 1,841
  • 5
  • 26
  • 42
  • Is `$_GET['trade']` empty? Are you trying to `echo` it *after* the `exit`? – Michael Jan 30 '14 at 11:23
  • you can call ajax for onchange event – Dinesh Jan 30 '14 at 11:31
  • you are trying to mix php and javascript, the php script does not live after you have done the load call so after you do the call there is no $ProductType, you would need to store it in some session variable. – Patrick Evans Jan 30 '14 at 11:33
  • I had tested your code it is perfect. when I am changing select box it show output as You selected: 2. You wrote `onchange` event for selectbox so it showing result when your select option changed to new option. – Gowri Jan 30 '14 at 11:35
  • Thanks everyone, I have added and EDIT in the hope that it clears up what I would like to achieve. – SixfootJames Jan 30 '14 at 11:37
  • when the form is submitted you would need to get `$_POST['trade']` as the form is using post method. If you are wanting to change the $ProductType you will need to set a session variable and then retrieve the session variable on form submit. – Patrick Evans Jan 30 '14 at 11:40

1 Answers1

2

PHP runs on the server, jquery (javascript) runs inside the browser of the client machine, which bascially means if you inspect the html your php has created for you from inside chrome or IE, all you see is HTML and you php code will be missing, it's missing because the server has run it (on the 'server side') and used it's output to change/append the html you have included in your php.

Your html form uses the 'post' method, so when the http protocol posts (sends back) to the server, you need to use $_POST["trade"] to access the value sent from the browser on postback , php post var documentation here

Usually one adds a hidden variable to one's html form which the php can use as a check, if the form has been posted back to the server, or if no postback was received (first run) then the hidden variable will not appear in the $_POST collection of variables

Community
  • 1
  • 1
Ninjanoel
  • 2,864
  • 4
  • 33
  • 53
  • Thanks Nnoel for that well description. Since changing from GET to POST, the page now reloads and echos the correct variable output. Now I just need to figure out how this session variable works from your links. – SixfootJames Jan 30 '14 at 12:13
  • session variables are useful, but if you just write everything into html form variables you can track php variables by reloading everything from the html postback, else yes, sessions are required to remember which page was delivered to which user, so the correct variables can be loaded. @SixfootJames – Ninjanoel Jan 30 '14 at 12:21