2016-11-04 26 views
-1

它怎麼可能得到jQuery的變量值PHP變量如何獲得jQuery變量值的PHP變量選擇變化事件沒有Ajax?

我的jQuery代碼

jQuery(document).ready(function(){     

     jQuery("#mec_make").change(function(){ 
     jQuery("#frame").html(''); 

     makeval = jQuery(this).val(); 
     jQuery("#sec").val(makeval); 
    }); 

}); 
</scipt> 

HTML

<input type="hidden" name="te" value="" id="sec"> 

我會盡量在我的PHP得到jQuery的價值代碼,但我不能得到它。

PHP

echo $year = '<script>jquery("#sec").val()</script>'; 
echo $year = '<script>document.write(document.getElementById("sec").value)</script>'; 

我不需要AJAX我希望它沒有Ajax,我需要它的時候改變事件火災值將存儲到PHP變量。

全碼

<div class="mak" id="mmm"><input type="hidden" name="te" value="" id="sec"></div> 

<?php 

echo "buddy".$year = '<script>jQuery("#sec").val();</script>'; 

$get_recent = $wpdb->get_results("SELECT * FROM wp_make_details 
          WHERE post_id =".$post_id." 
          AND year =$year 
          AND make_name ='Yamaha' 
          "); 

foreach ($get_recent as $key => $rec_value) { 
      $rec_value_year_data[] = $rec_value->year; 
      $model_name['model_name'] = $rec_value->model_name; 
      $datarrr[] =$model_name; 
} 

?> 

<select name="mec_year" id="car_year_select"> 
      <?php 
      $post_id = get_the_ID(); 
      $inserted_year = get_post_meta($post_id,'year',true); 
       echo "<option value=".$inserted_year.">".$inserted_year."</option>"; ?> 
        <script> 
        jQuery(document).ready(function(){ 
         jQuery("#mec_make").change(function(){ 
          jQuery("#frame").html(''); 
          makeval = jQuery(this).val(); 
          jQuery("#sec").val(makeval); 
         }); 
       }); 
      </script> 
</select> 
+0

'echo $ year;'...... – devpro

+0

'echo'it在賦值之後,不在賦值期間。 – Natrium

+0

@Natrium echoit分配後? – maddy

回答

1

如果你只想把mec_make價值爲隱藏字段id="sec"比你爲什麼要使用PHP在這裏,你可以得到所需的輸出爲:

jQuery(document).ready(function(){     
 

 
     jQuery("#mec_make").change(function(){ 
 
     jQuery("#frame").html(''); 
 

 
     makeval = jQuery(this).val(); 
 
     jQuery("#sec").val(makeval); 
 
     console.log(jQuery("#sec").val()) ; 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> 
 
<input type="hidden" name="te" value="" id="sec"> 
 
<select name="mec_make" id="mec_make"> 
 
    <option>Select</option> 
 
    <option value="1">My Value</option> 
 
</select>

另請注意,此</scipt>應該是</script>,修復錯字。

現在,你如何獲得在PHP中的值,簡單的答案使用<form>和提交按鈕,你會得到在PHP中的值。

+0

因爲我需要在我的SQL查詢中的值,所以我想在PHP變量中存儲該值。 – maddy

+0

@maddy:是的,現在你可以在這裏使用

,你會在PHP中獲得所有的值,你可以在你使用它們查詢 – devpro

+0

好吧,我已經使用表單,但我希望它沒有提交 – maddy