Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Ajax中,如何将html页面中的国家名称传递给php页面?_Javascript_Php_Ajax - Fatal编程技术网

Javascript 在Ajax中,如何将html页面中的国家名称传递给php页面?

Javascript 在Ajax中,如何将html页面中的国家名称传递给php页面?,javascript,php,ajax,Javascript,Php,Ajax,如何使用ajax将html页面中的国家名称传递到php页面 demo.php <html> <head> <title>Dynamic Form</title> <script src="http://code.jquery.com/jquery-1.10.1.min.js" ></script> <script> $(document).ready(function(){ $("form").on('s

如何使用ajax将html页面中的国家名称传递到php页面

demo.php

<html>
<head>
<title>Dynamic Form</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
<script>
$(document).ready(function(){
    $("form").on('submit',function(event){
    event.preventDefault();

        data = $(this).serialize();
        $.ajax({
        type: "GET",
        url: "post.php",
        data: data
        }).done(function( msg ) {
        alert( "Data Saved: " + msg );
        });
    });
});
</script>

</head>
<body>

<form>
<table>
<tr>
<td>
<select name="one" onchange="if (this.value=='other'){this.form['other'].style.visibility='visible';this.form['submit'].style.visibility='visible'}else {this.form['other'].style.visibility='hidden';this.form['submit'].style.visibility='hidden'};"



<option value="" selected="selected">Select...</option>
<option value="India">India</option>
<option value="Pakistan">Pakistan</option>
<option value="Us">Us</option>
<option value="other">Other</option>
</select>
<input type="textbox" name="other" id="other" style="visibility:hidden;"/>
<input type="submit" name="submit" value="Add Country"  style="visibility:hidden;"/>

</td>
</tr>
</table>
</form>

</body>
<?php

if(isset($_POST['submit']))
{
    $Country = $_POST['other'];
    echo $Country;
}
?>

动态形式
$(文档).ready(函数(){
$(“表格”)。在('submit',函数(事件){
event.preventDefault();
数据=$(this).serialize();
$.ajax({
键入:“获取”,
url:“post.php”,
数据:数据
}).done(函数(msg){
警报(“保存的数据:“+msg”);
});
});
});

你有一些错误
$\u POST['submit']
从不使用
数据=$(this.serialize())进行发布。所以你需要检查其他的东西。
另外,在AJAX中定义
GET
作为方法,但在PHP中选中
$\u POST
。因此,改变其中一个。 比如:

HTML/JavaScript:

<html>
<head>
    <title>Dynamic Form</title>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
    <script>
        $(document).ready(function(){
            $("form").on('submit',function(event){
                event.preventDefault();
                data = $(this).serialize();
                $.ajax({
                    type: "POST",
                    url: "post.php",
                    data: data
                }).done(function( msg ) {
                    alert( "Data Saved: " + msg );
                });
            });
        });
    </script>

</head>
<body>

<form>
    <table>
        <tr>
            <td>
                <select name="one" onchange="if (this.value=='other'){this.form['other'].style.visibility='visible';this.form['submit'].style.visibility='visible'}else {this.form['other'].style.visibility='hidden';this.form['submit'].style.visibility='hidden'};">
                    <option value="" selected="selected">Select...</option>
                    <option value="India">India</option>
                    <option value="Pakistan">Pakistan</option>
                    <option value="Us">Us</option>
                    <option value="other">Other</option>
                </select>
                <input type="textbox" name="other" id="other" style="visibility:hidden;"/>
                <input type="submit" name="submit" value="Add Country"  style="visibility:hidden;"/>
            </td>
        </tr>
    </table>
</form>

</body>

动态形式
$(文档).ready(函数(){
$(“表格”)。在('submit',函数(事件){
event.preventDefault();
数据=$(this).serialize();
$.ajax({
类型:“POST”,
url:“post.php”,
数据:数据
}).done(函数(msg){
警报(“保存的数据:“+msg”);
});
});
});
选择。。。
印度
巴基斯坦
美国
其他
PHP:


您可以试试这个

$(document).ready(function(){
$("form").on('submit',function(event){
var country = $("select[name=one]").val();
$.ajax({
type: 'POST',
dataType: 'json',
url: "post.php",
data: { country:country},
success: function(json) {
}
    });
});

$\u POST['submit']
不会被发布。去掉那个。另外,在PHP中检查
$\u POST
,但在AJAX中定义
GET
作为您的方法。所以,改变其中一个,它就会起作用。
$(document).ready(function(){
$("form").on('submit',function(event){
var country = $("select[name=one]").val();
$.ajax({
type: 'POST',
dataType: 'json',
url: "post.php",
data: { country:country},
success: function(json) {
}
    });
});