阿贾克斯张贴到同一页面上的PHP不工作 [英] Ajax post to php on same page not working

查看:113
本文介绍了阿贾克斯张贴到同一页面上的PHP不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个变种,从传递到jQuery的PHP。

研究后,我发现最常见的建议是这样与发表与Ajax和使用PHP存储后变种。

我的PHP技能pretty的不错,但我不能说我的JavaScript技能相同。

这是我在做什么,但似乎并没有在所有的工作:

  // jQuery的libray包括等等等等
  <脚本>
   $阿贾克斯({
      键入:POST,
      网址:http://currentpage.com
      数据:VAR =价值,
      数据类型:字符串
    });
 < / SCRIPT>
 < /头>


<身体GT;

< PHP
如果($ _ REQUEST [变种] ==值){
    回声变种通过与存储;
}
?>
 

解决方案

 <脚本>
   $阿贾克斯({
      键入:POST,
      网址:的index.php
      数据:{VAR:值},
      数据类型:文本,
      成功:功能(数据){
       //测试什么是从服务器返回
         警报(数据);
      }
    });
 < / SCRIPT>
 

I am trying to pass a var from jQuery to PHP.

After researching I found the most common suggestion was to do this with Post with Ajax and store the post var using PHP.

My PHP skills are pretty good but I can't say the same about my JavaScript skills.

This is what I am doing but does not seem to work at all:

  // jquery libray included etc etc
  <script>
   $.ajax({
      type: "POST",
      url: "http://currentpage.com",
      data: "var=value",
      dataType: string
    });
 </script>
 </head>


<body>

<?php
if($_REQUEST["var"] == "value") {
    echo "var passed and stored";
}
?>

解决方案

<script>
   $.ajax({
      type: "POST",
      url: "index.php",
      data: {var:'value'},
      dataType: 'text',
      success:function(data){
       // Test what is returned from the server
         alert(data);
      }
    });
 </script>

这篇关于阿贾克斯张贴到同一页面上的PHP不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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