1

Yes, there are lots of questions to similar stuff but I can't figure it out, sorry.

I have a file with some javascript variables, depending on user input (but no form) and a normal HTML link to my php file.

<script>
function doStuff() {

  var a = 'foo';
  var b = 'bar';

  window.location = 'newfile.php?a=' + a + '&b=' + b;
}
</script>

<a href="javascript:doStuff()">go to new php file</a>

That works fine, I can access the data in newfile.php with $_GET.

newfile.php:

<?php
$a= $_GET['a'];
$b= $_GET['b'];
echo($a,$b); // works
?>

But I'd like to use POST. I guess I have to use ajax for that but how exactly? jQuery is included btw so I could use $.ajax()

Any help is highly appreciated :)

EDIT:

Thanks for the quick response guys! The JSON parsing doesn't work, I can't really figure out why - after clicking on the button the browser window disappears for a split second and I'm on the same page again which is unresponsive now :(

I went with the following code:

jQuery.post('newfile.php',{'a': a, 'b': b}); //curious: with or without ''?
setTimeout(function () {
    window.location = 'newfile.php';
    }, 5000); //this will redirct to somefile.php after 5 seconds

newfile.php:

$a= $_POST['a'];
$b= $_POST['b'];

echo('Testing: '.$a);

Right after clicking I can see the correct output in Firebug (Testing: foo) but of course after redirecting to the site the values are lost and I'm left with "Testing: "

What can I do?

  • welcome to stackoverflow, if you find any of the answer to be resolving your issue, mark the one accepted by checking the green checkmark on the left side of the answer. – viral Jun 06 '15 at 07:19

3 Answers3

1

You can use ajax to achieve this. Following is the code which works on a button click or anchor click.

HTML

<button type="button" id="button1">Click me </button>

Ajax

$('#button1').click(function() {
var a = $('#IDofYourFormelement').val();
var b = $('#IDofYourFormSecondElement').val();

$.post('/somefile.php', {'somevariable': a, 'variableB': b}, function(data) {
    var parsed = JSON.parse(data);
    if (parsed == 'success') {
       setTimeout(function () {
        window.location = '/somefile.php';
        }, 3000);//this will redirct to somefile.php after 3 seconds
    }
    else
    {
        alert ('Invalid details');
    }
});
});

and then in your somefile.php you can access it as

$a = $_POST['somevariable'];
$b = $_POST['variableB'];
//do some stuff and return json_encoded success/failure message
viral
  • 3,724
  • 1
  • 18
  • 32
Khan Shahrukh
  • 6,109
  • 4
  • 33
  • 43
0

I guess you are trying to post the variables using javascript and display the page post executing your post variables. Found a similar question and an answer in here - JavaScript post request like a form submit.

EDIT

The window.location will call another instance of you page and then will assign or replace the current doc, hence your previous post parameters are lost. If you want the page with your post parameters passed you need to do a form submit to your php page with method=POST also with the post parameters. That's what is written in the above stackoverflow link I shared.

Community
  • 1
  • 1
debatanu
  • 766
  • 5
  • 11
  • Okay, if I need to build a form dynamically then I'll just stick with my $_GET solution I described at first. Thanks for the link anyway. – user2370903 Jun 07 '15 at 10:30
0

You can use the new with HTML5 FormData(); Code snippet from https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects see also https://developer.mozilla.org/en/docs/Web/API/FormData and http://caniuse.com/#feat=xhr2 for browser support

var formData = new FormData();

formData.append("username", "Groucho");
formData.append("accountnum", 123456); // number 123456 is immediately converted to string "123456"

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);
KevInSol
  • 2,560
  • 4
  • 32
  • 46