如何调用同一个页面中的AJAX页面?页面、AJAX

由网友(蔑王侯)分享简介:我有两个文件的 demo.php 和 post.php中。我该怎么办单页面,而不是两个页面。I have two files demo.php and post.php. How can I do in singlepage instead of two page.demo.php <...

我有两个文件的 demo.php 和 post.php中。我该怎么办单  页面,而不是两个页面。

I have two files demo.php and post.php. How can I do in single page instead of two page.

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: "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>

post.php中

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

我如何使用 post.php中数据 demo.php 不将数据从一个网页到另一个。

How can I use the post.php data in demo.php without passing data from one page to another.

推荐答案

更​​改AJAX的网址

Change the url of your ajax

$.ajax({
      type: "POST",
      url: "demo.php",
      data: data
 }).done(function( msg ) {
      alert( "Data Saved: " + msg );
 });

而在你的demo.php添加此

And add this in your demo.php

<?php
    if(isset($_POST['other'])) {
        $Country = $_POST['other'];
        echo $Country;
    }
?>
阅读全文

相关推荐

最新文章