0

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 ..

So this is the code I wrote in 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:

<?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.

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.

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.

Any suggestion is very appreciated. Thank you in advance.

EDIT: I modified the ajax as follows:

$.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.

Community
  • 1
  • 1
  • 1
    I think you don't need this line: data: new FormData(this). in page1.php you only use the specified params. And I think that new FormData(this) doesn't work, because 'this' is in this case an Image, because the click-function is on an Image with an id: leftUp -- but i am not sure – DannielR Mar 16 '15 at 10:53
  • 1
    What is the error coming...can you check in your browser developer console log ... – Edi 0 Mar 16 '15 at 10:57
  • I took it out that line from page0.php, but nothing happens. : ( –  Mar 16 '15 at 10:59
  • **Aze K**, the browser is not reporting any error. –  Mar 16 '15 at 11:02
  • 1
    @simo-zz why you include("page0.php"); in page 1? – Nishit Maheta Mar 16 '15 at 11:14
  • 1
    @simo-zz what you get in response ? – Nishit Maheta Mar 16 '15 at 11:14

1 Answers1

0

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
      }
});
Nahid Bin Azhar
  • 703
  • 1
  • 5
  • 18