PHP get select option value

2019-08-30 09:14发布

问题:

I have a javascript function that I have to switch it into php.

But in php I don't know how to make it work.

The function is getting value in a select box in the page and convert it into a url link when submit is pressed.

In javascript it's easy you can get the value of a select box without submitting or refreshing the page and put the value directly into the function.

But in PHP I can't get the select value without refreshing the page.

Example:

<form name="form" method="post" action="<?php getUrlValues('car', '31231h3', quantity='***') ?>"
<select name="quantity">
   <option value="1"></option>
   <option value="2"></option>
</select>

Whatever I select in the select box will directly display into the function. getUrlValues('car', '31231h3', quantity=option value

Is there a way to do that? Without refreshing page.

回答1:

PHP doesn't have any way to get the value of a form field without the client submitting the form. But you could submit to an alternate page, process the value, and redirect to the correct url. For example, assuming your target domain/page is "www.mydomain.com/process.php":

 <?php
    $redirect_url = "http://www.mydomain.com/process.php?car=" . $_GET['car'] . "&31231h3=" . $_GET['31231h3'] . "&quantity=" . $_POST['quantity'];
    header("Location: " . $redirect_url);
?>


回答2:

Something like this will do the job

<?PHP
$urlStr = "";
foreach($_GET as $k => $v)
$urlStr .= "$k=$v;";
?>

<form name="form" method="post" action="<?PHP echo $urlStr; ?>">