简体   繁体   English

使用来自jQuery的参数执行PHP代码

[英]Executing a php code with parameters from jquery

I need to call a php page page1.php with parameters through $_POST method from a jQuery code, without reloading the page from which I make the call, let's call it page0.php .. 我需要通过jQuery代码中的$ _POST方法通过参数调用php页面page1.php ,而无需重新加载进行调用的页面,我们将其称为page0.php

So this is the code I wrote in page0.php 这就是我在page0.php中编写的代码

<img id="leftUp" src="/imagenes/cursor_1.png" width="52" height="52" ondragstart='return false;'/>

<div id="activity"style="text-align:left;position:relative;left:120px;">
   <h3 style="color: yellow;"></h3>
</div>

<script>

  var $level = 1;

  $("#leftUp").click(function()
  {
    var $hOffset = -5;
    var $vOffset = -5;

    $hOffset *= $level;
    $vOffset *= $level;

    $("#activity").text("Values h: " + $hOffset + " - v: " + $vOffset);

    event.preventDefault();
    $.ajax(
    { 
      url: "page1.php",
      method: 'POST',
      data: new FormData(this),
      params:
      {
        interrupt: '14',
        hOffset: $hOffset,
        vOffset: $vOffset
      },
      contentType: false,
      cache: false,
      processData: false,
      success: function(data)
      {
          alert('Success');
      },
      failure:function(transport)
      { 
        alert('Error');
      }
    });
  });
});

</script>

And the code for page1.php is basically this: page1.php的代码基本上是这样的:

<?php 
if(isset($_POST["interrupt"]))
{
    $interrupt = urlencode($_POST["interrupt"]);

    /* 
      write the interrupt number into the interrupt
      reference file
    */
    exec("echo '$interrupt' > $intFile"); 
}

if(isset($_POST["hOffset"]))
{
    $hOffset = urlencode($_POST["hOffset"]); 
}

if(isset($_POST["vOffset"]))
{
    $vOffset = urlencode($_POST["vOffset"]); 
}

/* other stuff here ... */

include("page0.php");
?>

I previously did something similar in the thread: ' Upload a file and reloading a div instead reloading a page ', but my code is not working at all. 我以前在线程中做过类似的事情:“ 上传文件并重新加载div而不是重新加载页面 ”,但是我的代码根本无法正常工作。

The content of the div activity is successfully updated, and the pop-up window of ' Success ' string is shown, but the PHP code seems to be not executed. div 活动的内容已成功更新,并且显示了“ Success ”字符串的弹出窗口,但PHP代码似乎未执行。

The problem should be into the jQuery code inside the $.ajax block, but I am not an experienced web programmer, so I really do not know how I can solve this. 问题应该在$ .ajax块中的jQuery代码中,但是我不是一位经验丰富的Web程序员,所以我真的不知道如何解决这个问题。

Any suggestion is very appreciated. 任何建议都非常感谢。 Thank you in advance. 先感谢您。

EDIT: I modified the ajax as follows: 编辑:我修改了ajax ,如下所示:

$.ajax(
{ 
  url: "page1.php",
  type: 'GET',
  data: 
  {
    interrupt: "14",
    hOffset: $hOffset,
    vOffset: $vOffset
  },
  contentType: false,
  cache: false,
  processData: false,
  success: function(data)
  {
      alert('Success');
  },
  failure:function(transport)
  { 
    alert('Error');
  }
});

But still nothing happens. 但是仍然没有任何反应。

you no need to 你不需要

data:new FormData(this)

try this 尝试这个

 $.ajax(
    { 
      url: "page1.php",
      method: 'POST',
      data: 'interrupt=14&hOffset='+hOffset+'&vOffset='+vOffset,
success:function(response){
//do something
},
 failure:function(transport)
      { 
        //do something
      }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM