1

So basically my question is very simple, I have two buttons, I for page forward, one for page backwards, If one of those is pushed, a javascript function is called inside an onClick Event. Javascript then gets the variables of the page and then redirects to the next page, the only problem is, that I need to pass those variables to PHP in order to put them into the Database. So for that I make a load of cookies to pass the variables.

However, I was wondering if something like this would work :

<form>
<a onClick="nexpage();" onSubmit="phpScript.php"> <img src = "previous button.jpg"/> </a>
</form>

The idea behind this is that I want to store the variables in a PHP script, which will put them in a display:none; <div> and then for javascript to get the variables out. This instead of using cookies.

So is it possible to run a PHP script to get the variables and when the script is finished to get them, Javascript kicks in to redirect to the next page...

The reason I don't test this at this moment, is that my code is 100% complete, I don't want any sudden changes that maybe won't work at all. Yes I know back-up this and that, but I thought just asking here, maybe someone will know the answer!

Sincerly, Harmen Brinkman

notknown7777
  • 419
  • 1
  • 7
  • 19
  • I dont think `onSubmit="phpScript.php"` will work – Prasanth Bendra Feb 28 '13 at 09:33
  • Why don't you just use `' and have the phpScript.php log the data and handle the redirection? – yourdeveloperfriend Feb 28 '13 at 09:34
  • 1
    You can't *submit a link*. Also `onXxx` always contain JavaScript. Maybe you should take a look at AJAX. – Fabian Schmengler Feb 28 '13 at 09:35
  • If it is a form submit it in your js functions, if not you may make an ajax request there (Would prefer this option) to the php script which handles this. – Hikaru-Shindo Feb 28 '13 at 09:35
  • I share the same opinion as @fab. First submit the form and send it to a javascript function. Then you can send an AJAX post call to the PHP page that needs the form information. Inside the AJAX call, on SUCCESS, you can then forward to the next page with window.href = "myPhpPage.php". – Ace Feb 28 '13 at 16:03

3 Answers3

2

You can also use onClick = "this.form.submit(); return false;".

There is no any event like onSubmit for link, instead form do have onSubmit event.

Normal Way as OP asked.

<form action = "phpScript.php" method = "POST">
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

you can use document.getElementById("my_form").submit();

Deblina
  • 11
  • 1
  • @Debina – Good point. However, that only submits the form, without you being able to intercept the code, send it to another PHP page and then forward to yet another PHP page. UNLESS you 1) set the form's method to POST and 2) set the form's action to go to the first PHP page – which could then process everything and forward the user to the next page. Theoretically, that would work. Not so sure what kind of user experience that would be, though. – Ace Feb 28 '13 at 16:15
0

@Dipesh Parmar – Good point. You could also do:

window.onload=function() {
  document.getElementById('my-form').onsubmit=function() {

    // do what you want with the form

    // AJAX POST CALL TO PHP PAGE

    // Should be triggered on form submit
    alert('hi');

    // You must return false to prevent the default form behavior
    return false;
  }
});

Inspiration by Capture a form submit in JavaScript

Community
  • 1
  • 1
Ace
  • 664
  • 4
  • 10
  • 20