简体   繁体   中英

how to get the form input, select values from the same page in php?

i have a form which has :

<form name="myForm" action="a.php" method="post">
    <fieldset>
        <label>class</label>
        <select>
            <option>1</option>
            <option>2</option>
            <option>3</option>
        </select>

        <label>Subject</label>
        <select>
            <option>some value from database</option>
            <option>some value from database</option>
            <option>some value from database</option>
            <option>some value from database</option>
        </select>
    </fieldset>
<form>

the thing is:

i want to show the 3 options(1,2 and 3) from the first drop down select values for the user. Suppose the user selects 1 then s/he will get corresponding values from database as the drop down values of the 2nd select. Then s/he will select one from these drop down values. finally the form will be submitted manually. Obviously depending on the first drop down select value, the second drop down select value will change accordingly.

So the challenge is to take the value from the first select drop down values to use it in the second select drop down values BEFORE THE FORM IS SUBMITTED MANUALLY. How can I do that? I am using php. What else do I need?

ADDED LATER:

so far I have got idea about the solution of the posted prob.

NOW think that,

I have to retrieve values for the second drop down list depending on the first one. If have a third drop down value to retrieve from database depending on the first and second one , and then a fourth drop down value depending on the first three , and so on for the fifth, sixth ... then how can I implement that please?

all the drop down <select></select> elements will sit side by side horizontally on the same page.

You need client-side scripts for it. Here is an article how to implement this feature using jQuery (popular JS framework)

you need to use javascript and AJAX to fetch the values for the second dropdown dynamically. For example using jQuery it would be:

$('#select1').change(function() {
   var selectedVal = $(this).val();

   //make an AJAX request to get your values depending on selectedVal
   //and update the values for the second select
}

In a PHP script you would then have to evaluate an incoming parameter representing the user selection for the first select and then return, for example in JSON format, the data you load from the database.

Edit: The AJAX request would then be made like this:

$.post("getsecondvals.php", { value: selectedVal }, function(data) {
   var result = $.parseJSON(data);
   $('#select2').html(''); //delete any old values

   for (var entry in result) {
      var option = $('<option></option>');
      option.text(entry);

      $('#select2').append(option);
   }
});

And your PHP script could be structured like this:

<?php
$selectedValue = $_POST['value'];
// now include everything required for your database connection
// and get the categories, use prepared statements

// finally, your resulting array should be non-associative, so
// always add new values with "[]" operator
// assuming $values is your array containing the valid options, do this:
echo json_encode($values);
?>

您将需要Javascript来处理此问题,重写List ControlonSelectionChange事件,并在change事件上从服务器端脚本获取第二个下拉框的值。

You can try this :

onchange = "$('select[name=\'xyz\']').load('yourUrl/someFunction&country_id=' + this.value);"

this onchange attribute will be applied to first select box and give name to 2nd select box like :

The above code will send an AJAX GET request whenever you change the option from first drop down menu.

And fetch values from DB in the function which is called and return HTML. Eg:

  someFunction() {
     //get values from DB and loop
   String html = "<option></option>";
   return html;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM