PHP Ajax回调从我的PHP指定的数据 [英] PHP Ajax callback specified data from my php

查看:416
本文介绍了PHP Ajax回调从我的PHP指定的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这里是我的问题;

OK , here is my issue;

我的问题是,当我回调我的PHP文件AJAX调用我的文件的所有内容

My issue is that when i callback my php file with ajax it calls all the content of my file

这是我的code

包括我view.php文件我source.php文件,该文件

my source.php file which including my view.php file

<?php
    if(isset($_POST['test'])){
       echo "OK";
    }
  include_once "view.php";
?>

我的view.php文件

my view.php file

<!DOCTYPE html>

<html>

<head>
  <title>Hello!</title>
</head>

<body>


<h1>Form</h1>

<form action="" method="post" id="form">
    <input type="text" name="test" id="test" /><br>
    <input type="submit" id="sub" name="sub" />
</form>

<div id="result"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
    $(document).ready(function(){
       $("#sub").click(function(){


        $.ajax({
           type: "POST",
           data: $('#form').serialize(),
           success: function(slider_data){
             $("#result").text(slider_data);
           }
        });

          return false;
       });
    });
</script>


</body>
</html>

现在,当我提出我的表格 我#result这里是

Now when i submit my form my #result here is

OK<!DOCTYPE html>

 <html>

 <head>
 <title>Hello!</title>
 </head>

 <body>


 <h1>Form</h1>

 <form action="" method="post" id="form">
 <input type="text" name="test" id="test" /><br>
 <input type="submit" id="sub" name="sub" />
 </form>

 <div id="result"></div>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

 <script>
 $(document).ready(function(){
 $("#sub").click(function(){


 $.ajax({
 type: "POST",
 data: $('#form').serialize(),
 success: function(slider_data){
 $("#result").text(slider_data);
 }
 });

 return false;
 });
 });
 </script>


 </body>
 </html>

我想只显示单词OK或什么的,我想和prevet发送的所有数据,其中包括文件​​

i want to show only the word "OK" or whatever i want and prevet sending all data and included file

推荐答案

因为你有包括 source.php 这就是为什么你得到 view.php 了。

because you have include in source.php that's why you are getting view.php again.

更改code这样

source.php

source.php

<?php
    if(isset($_POST['test'])){
       echo "OK";
    }
?>

&LT;脚本&GT; 在view.php会是这样

<script> in view.php will be like this

<script>
    $(document).ready(function(){
       $("#sub").click(function(event){
        event.prevantDefault();

        $.ajax({
           url:"source.php" // url to source.php 
           type: "POST",
           data: $('#form').serialize(),
           success: function(slider_data){
             $("#result").text(slider_data);
           }
        });

          return false;
       });
    });
</script>

这篇关于PHP Ajax回调从我的PHP指定的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆