简体   繁体   中英

use selected dropdown box value as variable in php

I would like to use the selected value in a drop-down box as a PHP variable in my functions.php file. I tried using jquery-ajax

 $(document).ready(function(){ $("#pa_color").change(function(){ var result= $(this).val(); document.getElementById("demo").innerHTML = result; if (result.length == 0) { document.getElementById("tit").innerHTML = ""; return; } else { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("tit").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "selectcolor.php?q=" + color, true); xmlhttp.send(); }); }); 

and can get the variable to show on the site using getElementById("demo") but get a 404 message when trying to call and process the variable using my selectcolor.php file. I would prefer using php only but will use whatever is necessary. I can get all the colors associated with each item using PHP

 $color = $product->get_attribute('pa_color'); 

but I only want the selected color. As you can see I'm new at this and my knowledge is limited. Thank you

$('#pa_color').val() 

Will output the selected value of the drop down element.

Use this to get the selected options text.

$('#pa_color option:selected').text();

Now you can use this value to be passed to your php file using AJAX or something.

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