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';
4
  • Just to clarify, it should return the value as soon as the user selects an option? As in on the change event? Commented Aug 9, 2018 at 18:27
  • @IsaacAbramowitz Yes please Commented Aug 9, 2018 at 18:46
  • stackoverflow.com/questions/2866063/… 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. Commented Aug 9, 2018 at 18:51
  • @IsaacAbramowitz This isn't text input type form but select type form please help Commented Aug 9, 2018 at 18:58

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Please little more explaining may be PHP side of thigs
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.