简体   繁体   中英

php variable won't populate onchange

I am trying to get the value of a drop down selection box this is he code i have tried to do it with as it is similar to how i added data to the database using $_POST I am unsure why it isn't loading into the variable I have tried echoing the var_dump and it just returns null.

<select name="raceTrack" onchange="<?$track = $_POST['raceTrack']?>">

I am unsure of why this is happening and how to fix it I have searched Google and can't seem to find anything.

You are mixing PHP and JavaScript. PHP executes at the server and doesn't care what the user does at their end. Javascript executes on the users machine and doesn't care what the server is doing.

You need to combine the two, with something like sending an OnChange back to the server to be able to use the variables in your PHP code - or simply have JavaScript do what it is you need doing.

onChange是JavaScript功能,除非您进行AJAX调用,否则无法与PHP通讯

The browser cannot perform PHP commands in an onChange. You may only use javascript there.

PHP processes on the server-side. You are attempting to execute it with a JavaScript (client-side) call. At the stage of the call the PHP has already been executed.

What you should do is send an AJAX request containing the value of the variable in the query string or POST body.

The PHP script at the other end of the URL can read the value from the query string or POST body and then you'll be able to do whatever you want with that variable.

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