[英]How to pass variable in javascript to php with ajax
I'm trying to pass the selected month from select list in html with ajax. 我试图用ajax从html的选择列表中传递选定的月份。 But every time I select a month, it didn't appear.
但是每次我选择一个月时,它都不会出现。 Before I select a month there's a "nothing" because of
if else
conditional, then when I select a month the "nothing" word dissapear but no variable fetched. 在我选择一个月之前,因为没有
if else
条件,所以没有“什么”,那么当我选择一个月时,“什么都没有”这个词消失了,但是没有获取任何变量。 Here's my javascript code: 这是我的JavaScript代码:
$('#button-search-ohr').on('click', function() {
var month = $('#monthChoose').val();
if(month == 0) {
alert('Month must be choosen!');
}
$.ajax({
async : true,
type : 'POST',
url : '<?php echo base_url();?>sms/lapkinerjastat',
data : {month:month},
success : function(data) {
$('#month').html(data);
}
});
}
Here's the html and php 这是html和php
<h5 class="pilih-bulan">Month Choose
<select id="monthChoose" name="month">
<option value="0">Pilih bulan</option>
<option value="Januari">Januari</option>
<option value="Februari">Februari</option>
<option value="Maret">Maret</option>
<option value="April">April</option>
<option value="Mei">Mei</option>
<option value="Juni">Juni</option>
<option value="Juli">Juli</option>
<option value="Agustus">Agustus</option>
<option value="September">September</option>
<option value="Oktober">Oktober</option>
<option value="November">November</option>
<option value="Desember">Desember</option>
</select>
<button id="button-search-ohr" type="submit">Search</button>
</h5>
<div id="month">
<?php
if(isset($_POST['month'])) {
$month = $_POST['month'];
echo 'This is '.$month;
}else {
$month = null;
echo 'Nothing';
}?>
</div>
How do I do the proper way to passing variable in javascript to store that variable in php? 如何在JavaScript中传递变量以将其存储在php中的正确方法?
EDIT : I realized I wrote the wrong url
and I've just edited the url
in javascript. 编辑:我意识到我写了错误的
url
,我只是用javascript编辑了url
。 It successfully pass the variable but the page become double. 它成功传递了变量,但页面变成了两倍。 Here I got the screenshot.
在这里,我得到了屏幕截图。 Before select month (pay attention at "Nothing")
选择月份之前(请注意“无”)
After select month (pay attention at "This is Februari") 选择月份后(请注意“ This is Februari”)
EDITED ANSWER 编辑答案
When you click on your button you submit the form so you are redirected on same page. 当您单击按钮时,您将提交表单,以便您在同一页面上被重定向。
Try this : 尝试这个 :
$('#button-search-ohr').on('click', function(e) { // Add the "e" in your function
// You stop the submit
e.preventDefault();
e.stopPropagation();
var month = $('#monthChoose').val();
if(month == 0) {
$('#month').html('Nothing');
alert('Month must be choosen!');
} else {
$('#month').html('This is ' + month);
}
}
And replace : 并替换:
<div id="month">
<?php
if(isset($_POST['month'])) {
$month = $_POST['month'];
echo 'This is '.$month;
}else {
$month = null;
echo 'Nothing';
}?>
</div>
By : 由:
<div id="month">
Nothing
</div>
You don't need to make an Ajax call to the same page to change the content of your div. 您无需在同一页面上进行Ajax调用即可更改div的内容。 Just switch from "Nothing" to the "This is " + the selected month.
只需从“无”切换到“这是” +所选月份。
Here is a snippet to show you : just click on your button and the content change : 这是向您展示的摘录:只需单击您的按钮,内容就会发生变化:
$('#button-search-ohr').on('click', function(e) { // Add the "e" in your function // You stop the submit e.preventDefault(); e.stopPropagation(); var month = $('#monthChoose').val(); if(month == 0) { $('#month').html('Nothing'); alert('Month must be choosen!'); } else { $('#month').html('This is ' + month); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h5 class="pilih-bulan">Month Choose <select id="monthChoose" name="month"> <option value="0">Pilih bulan</option> <option value="Januari">Januari</option> <option value="Februari">Februari</option> <option value="Maret">Maret</option> <option value="April">April</option> <option value="Mei">Mei</option> <option value="Juni">Juni</option> <option value="Juli">Juli</option> <option value="Agustus">Agustus</option> <option value="September">September</option> <option value="Oktober">Oktober</option> <option value="November">November</option> <option value="Desember">Desember</option> </select> <button id="button-search-ohr" type="submit">Search</button> </h5> <div id='month'> Nothing </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.