2

I have a drop-down select menu which I'll want to use to return the value of a variable without a submit button and no page reloads. Index.html

<form action="fetch" method="get">
          <p>
            <select name="POS">
              <option>SELECT COURSE</option>
              <option value="POS">Agricultural Economics and Extension</option>
              <option value="A">Another</option>
            </select>
          </p>
          <p>
            <input type="submit" name="check" id="check" value="Submit">
          </p>
        </form>

PHP Page

if(isset($_GET['check'])){

$POS = 'Agricultural Economics and Extension';
$CC  = 'English, Mathematics, Chemistry';
$CO  = 'Either Biology or Agricultural Science';
$OS  = 'Any one of Physics, Geography and Economics';

}

Let the value of the drop down example

<option value="POS">Agricultural Economics and Extension</option>

Return the defined value in PHP without reloading the page.

$POS = 'Agricultural Economics and Extension';
PULabs
  • 39
  • 2
  • 9
  • Just to clarify, it should return the value as soon as the user selects an option? As in on the `change` event? – Isaac Abramowitz Aug 09 '18 at 18:27
  • @IsaacAbramowitz Yes please – PULabs Aug 09 '18 at 18:46
  • https://stackoverflow.com/questions/2866063/submit-form-without-page-reloading This is a question regarding submitting forms without reloading the page. I'm not sure if you can return a value without submitting a form, and I'm not able to test it right now since I don't have access to a tool to create a local server. But as far as I know, a form must be submitted to get a value through PHP. – Isaac Abramowitz Aug 09 '18 at 18:51
  • @IsaacAbramowitz This isn't text input type form but select type form please help – PULabs Aug 09 '18 at 18:58

1 Answers1

1

It looks like you are using jquery so you can just use the jquery get function to do this.

 <select name="POS" onChange="getUpdate(this)">
      <option>SELECT COURSE</option>
      <option value="POS">Agricultural Economics and Extension</option>
      <option value="A">Another</option>
 </select>


<Script language="Javascript">
function getUpdate(selectObject) {
    var value = selectObject.value;  

    $.get( "urlToyourPHPResults.php?POS="+value, function( data ) {
    // Do something with returned data
    });

}
</script>
pg316
  • 1,380
  • 1
  • 8
  • 7
  • Please little more explaining may be PHP side of thigs – PULabs Aug 09 '18 at 18:59
  • In this example you should be able to just use $_GET["POS"] to get the data and then you can output it back in whatever format you want. For instance in json, xml or just text. header('Content-Type: application/json'); echo json_encode(array('POSData' => 'data')); You can then pull the data like var jsonData = data.POSData – pg316 Aug 10 '18 at 16:36