1

I have a simple form on my homepage (index.php), that takes one user input.

<form action="/run.php" method="POST" target="_blank"
    <input type="text" name="userinput">
    <button type="submit">Run!</button>
</form>

That input is then passed to run.php where the content is displayed and inserted into a MySQL database.

However, I need to run JavaScript functions on that user input, and then input the results (from the JavaScript function) into the database (so passing the value from JavaScript to PHP).

I originally had the JavaScript in the run.php, which worked for calculating and displaying the value, but I was unable to pass the value to PHP to insert into the database.

From what I've read, you can't pass JavaScript values to PHP on the same page, as it requires some sort of POST or GET, so I'm attempting to run the JavaScript functions on the homepage index.php and then use a hidden input field to POST the value to the run.php file.

<input id='hidden_input' type='hidden' name='final-calc' value='random(userinput)' />

Where the function is a JavaScript Promise:

function random(userinput) {
    ....
    .then(function(userinput) { // Function needs to use the userinput from the form
         // calculations
         return X //(X being any random value)
    }
}

The two problems I'm running into is that:

  1. I can only get the userinput value after the user enters a value and submits the form, so I don't believe I can pass that userinput value into the JavaScript function and still POST the returned value?

  2. The JavaScript function is an asynchronous Promise, but apparently, this might not have an effect - so it may not be a problem.

Pebble
  • 47
  • 8
  • From what I understand, you want to POST some vars in a PHP script and then act on these vars using JS. If that is the case, why don't you make the calculations at a first stage with JS and then POST the results to PHP? – Arkoudinos Mar 09 '17 at 10:31
  • @Arkoudinos *why don't you make the calculations at a first stage with JS and then POST the results to PHP?* That's what I'm trying to do. However, the JS functions need the `userinput` for the calculations. So I don't know how to get the `userinput` and then POST the results afterwards. – Pebble Mar 09 '17 at 10:34
  • Try this,http://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable – Kaushar Alam Mar 09 '17 at 10:38
  • @KausharAlam Thanks for the resource. How can I ensure the JavaScript only runs after the form is submitted? Because the function needs the userinput value to do the calculations. – Pebble Mar 09 '17 at 10:44

1 Answers1

0

The key here is AJAX. You have to retrieve the userinput using plain JS, make any calculations needed and then send the results to your PHP script. I left a fiddle: https://jsfiddle.net/k5xd54eu/1/

<input type="text" id="userinput" name="userinput"/>
<button onclick="inputhandler();">TEST</button>

<script>
function inputhandler() {
  var text = document.getElementById('userinput').value;
  alert(text);

  /*DRAGONS BE HERE*/
  /*THEN*/
  /*USE AJAX HERE TO PASS THE RESULTS TO PHP SCRIPT*/
}
</script>

I'm not explaining how to implement the AJAX call to send the results, mainly because it's a matter of taste too. For example you can use plain JS or a library such as jQuery (which is my personal preference, since it's very clean and easy). Take a look here:

http://api.jquery.com/jquery.ajax/

and here:

https://www.w3schools.com/jquery/jquery_ajax_intro.asp

for more information.

EDIT: Since I've mentioned AJAX, it would be more correct to include some code. So this is what I generally use for simple POSTs (it's based on the jQuery library so be careful to include it):

var url = 'ajaxhandler.php';
var stuff = '1234abcd';

$.ajax({
        type: "POST",
        url: url,
        data: {stuff: stuff},
        success: function (data) {
           /*Do things on successful POST*/
        }
        });
Arkoudinos
  • 1,099
  • 12
  • 20
  • Thanks for this suggestion. However I'm facing an issue with it. The script runs, so if I put `alert("success");` in the `success` part of the ajax script, then an alert will show when the form is submitted, however, I can't seem to use that data on the *run.php* file, even if I try something simple like `` (where `data: { ajaxdata: XYZ }` – Pebble Mar 09 '17 at 12:48
  • On Chrome, open the Dev Tools by pressing F12. Then click on network tab and then again, on the XHR tab below. There you will find your AJAX request among with what vars have you posted. Have a look, it could be really helpful. – Arkoudinos Mar 09 '17 at 13:15
  • Nothing particularly interesting jumped out from the network tab. However, one interesting issue(?) is that if I use the AJAX code you wrote above and add `alert(stuff)` (even if I change the variable names to be different) then the alert shows the entire source code of the `run.php` page. – Pebble Mar 09 '17 at 13:28
  • I am updating the code I provided you. I can confirm 100% that this one works flawlessly. On the XHR tab click on your request, and check what response you get from the server (aka your PHP script). – Arkoudinos Mar 09 '17 at 13:43
  • That still doesn't work for me, unfortunately. Even if I add exactly what you've written (just changing the url to *run.php* and then add `` in my *run.php* file, nothing is shown. – Pebble Mar 09 '17 at 17:14
  • In the network tab on my *index.php* page (the page with the form & AJAX) it shows *run.php* as one of the requests, and if I click it then it shows the data `1234abcd` that's supposed to be sent. I just can't seem to access it on my *run.php* page – Pebble Mar 09 '17 at 17:21
  • I think I know the issue (but not how to fix it)! When the form is submitted it takes you to a random url (that's using the *run.php* script). So AJAX is sending it directly to `example.com/run.php` instead of `example.com/abcxyz` that the form redirects to. – Pebble Mar 09 '17 at 17:25
  • Random URL?! Then something else is really wrong. Once again, I retested the code I gave you with a simple server-side script (a one-liner, really), and it works like a charm. The PHP code is: and here is the response: http://imgur.com/a/oSAX8 – Arkoudinos Mar 09 '17 at 20:12
  • Sorry, just to clarify, the form **is** supposed to submit to a random URL (that's running *run.php*) (as should the AJAX data). It doesn't really change the issue (I know how to submit to the random URL). – Pebble Mar 09 '17 at 21:01
  • It's strange - the `response` in the `network` tab works (it shows the result in the place I put ``), however, it doesn't do the same thing on the actual url (only works in the network tab, not in the webpage). I don't think I'm explaining the issue very well. – Pebble Mar 09 '17 at 21:05
  • Since you get the correct response, it means that your PHP script works fine and the problem lies somewhere else. Maybe it's something trivial, maybe not. TBH I'm not sure I (or anyone else) could help you without seeing a good part of your code and/or configuration. – Arkoudinos Mar 10 '17 at 00:05