Reputation: 25
I want to use the select value in current PHP file, but without submit button, how do I do this?
This is my code:
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
);
$pays = get_terms( 'pay', $args );
$html = '<form action="#" method="post"><select name="selected_pay" id="selected_pay" >';
foreach ($pays as $pay) {
$html .= '<option value="'.$pay->name.'">'.$pay->name.'</option>';
}
$html .= '</select></form>';
echo $html;
?>
And I want to use it like this:
<?php
if (isset($_POST['selected_pay']))
echo $_POST['selected_pay'];
else
echo 'Please select a pay';
?>
Upvotes: 2
Views: 6790
Reputation: 25
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
);
$pays = get_terms( 'pay', $args );
?>
<form method="post">
<label for="combineForm">Un Pays</label>
<select name="selected_pay" id="selectedPay" onChange="form.submit()">
<?php
foreach ($pays as $pay) : ?>
<option value="<?php echo $pay->name; ?>"><?php echo $pay->name; ?></option>
<?php endif; endforeach;?>
Upvotes: 0
Reputation: 364
You do not need a submit button, but anyway you need to send to the server what is the current value of selected_pay.
You can do it on some event for example. Like hovering mouse over some element or even on some specific time ;) But you have to define the event - when is the moment you want to send/check that if not on clicking on a submit button.
I guess what you really want is to send/check that value whenever it changes using AJAX. You can use JQuery with onchange event.
jQuery('#selected_pay').change(function(){
jQuery.post('script.php', {selected_pay: jQuery(this).val()}, function(data){
jQuery(this).append(data); // show the result from script.php under the select element
})
});
Upvotes: 1