2

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';

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Weiwei Han
  • 25
  • 1
  • 7
  • You can't do this without some kind of submit function. If a classic submit isn't right for you, you could look up ajax submission – Philip G Mar 06 '15 at 11:08
  • Philip G is correct, you need ajax. Look here: http://stackoverflow.com/questions/17591582/set-phps-session-without-form-submit – Yavor Mar 06 '15 at 11:09
  • But I don't know about ajax, can you give me some idea guys? – Weiwei Han Mar 06 '15 at 11:34

2 Answers2

1

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  
   })

});

magento4u_com
  • 364
  • 2
  • 6
0
<?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;?>

Weiwei Han
  • 25
  • 1
  • 7