Question

I've seen a lot of examples of the same but it just doesn't work for me! I really don't know what is wrong with my code. When I perform a post via

window.location.href = "teste.php?name=" + javascriptVariable; 

it work perfectly, but sadly, reloads the page, and I really don't want it. So the only solution I've seen was to do it via jQuery. So here is what I am doing.

<script>
function opa() { 
  //var javascriptVariable = "John";
  //window.location.href = "teste.php?name=" + javascriptVariable; 
  //alert (dataString);return false;  
  var dataString = "axius";
$.ajax({  
 type: "POST",  
  url: "http://localhost/teste.php",  
  data: {
                name : dataString
            },  
  success: function() {  
    alert("postado!");
  }  
});  
return false;  
}

function opa2() { 
  alert("<?php
if(isset($_POST['name'])){
    $value = $_POST['name'];   
} else {
    $value = "NotWorking";
}
echo $value;
?>");
}

</script>

<button onclick="opa();"> AperteOpa1 </button> 

<button onclick="opa2();"> AprteOpa2 </button> 

The POST works PERFECTLY, when i see it at the web console at Firefox, it happens pretty well, and i can see the values at the parametters. I think the problem is with the PHP that don't recognize the data. I've tried to perform POST request trough xmlhttp.open(); but it didn't worked too, same problem, the post happen, but the php don't recognize... what's wrong with the code?

Was it helpful?

Solution

Your code does work, But it never gets called.

The ajax call doesnt reload the page, So when the page was processed, there was no $_POST['name'] there.

The ajax version of the page will have that variable set, but the one your currently running in does not.

Your process is

GET PAGE =>                         => show post name
            AJAX Request => Post => 

It should be

GET PAGE =>
            AJAX Request => post => show post name

As you can see, The Ajax request doesnt touch the post name in your version. You need to get the data from the ajax request and then do something with that. Not the original page version

Your post should return something javascript can parse, Either plain text or JSON

OTHER TIPS

The problem is that the page is generated by PHP first while the POST variable isn't set. The ajax request does not cause the page to reload, so it doesn't get regenerated, so this code never sees the submitted data.

I'm not sure what you're really trying to accomplish, so I can't suggest the proper way to fix this, but it seems you have an incomplete understanding of how and when server side and client side processing happen.

The problem is that you are posting to a different script (teste.php) and not the same script you are trying to run the javascript from.

While some say that you can't mix PHP and Javascript, I disagree. PHP happens before (on the server side) so your intuition was ok. The only problem is that the $_POST will not contain any value (unless you access it from teste.php).

You need to send the data back from the php file to the success function:

success: function(response) {
   alert(response);
}

Also, just for general information, I'd rather replacing

if(isset($_POST['name'])){
    $value = $_POST['name'];   
} else {
    $value = "NotWorking";

with

$value = isset($_POST['name']) ? $_POST['name'] : 'NotWorking';

It is much cleaner!

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top