无法获得AJAX帖子以将ID值发送到PHP页面

时间:2014-09-26 01:53:24

标签: php jquery html ajax

我在不同的输入上使用了相同的代码,它工作正常。但是,我不能让这个工作。我无法让post动作从选择菜单中发送值并回显变量的值。

HTML code:

<select type="text" id="typeofpayment" class="form-control" placeholder='Payment Method'>
<option>Choose Payment Method</option>
<option value= "1">Cash</option>
<option value="2">Check</option>
<option value="3">Credit Card</option>
<option value="4">Paypal</option>
</select>           
<div id='loadinvoice'></div>
<div id='loadinvoice2'></div>

JS脚本

(document).ready(function(){
$('input#datepaid').on('change',function(){
$.post( "typeofpayment.php", $( 'select#typeofpayment' ).serialize(), function( data ){
alert( "Data Loaded: " + data );
$( "#loadinvoice2" ).html( data );
});
});
});

PHP代码

<?php
echo 'hello';
$typeofpayment= $_POST['typeofpayment'];
echo '$typeofpayment';
?>

1 个答案:

答案 0 :(得分:0)

试试这段代码: Html代码:

<select type="text" id="typeofpayment" name="typeofpayment" class="form-control" placeholder='Payment Method'>
<option>Choose Payment Method</option>
<option value="1">Cash</option>
<option value="2">Check</option>
<option value="3">Credit Card</option>
<option value="4">Paypal</option>
</select>
<div id='loadinvoice'></div>
<div id='loadinvoice2'></div>

Js代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script>
    $(document).ready(function(){
        $('#typeofpayment').on('change',function(){
        $.post( "typeofpayment.php", $( 'select#typeofpayment' ).serialize(), function( data ){
            alert( "Data Loaded: " + data );
            $( "#loadinvoice2" ).html( data );
            });
        });
    });
    </script>

PHP文件:

<?php
echo 'hello ';
$typeofpayment= $_POST['typeofpayment'];
echo $typeofpayment;
?>