-1

I have a fairly complex PHP script in place, and I need to embed a very small JavaScript prompt inside of this code.

This is my criteria / requirement:

  1. I want this JavaScript code to execute when a certain set of criteria is met.
  2. I do NOT want to execute this code with any kind of submit button.
  3. I currently have the PHP code calling the JavaScript prompt correctly.
  4. The JavaScript comment / variable is being initialized, and stored properly, within the JavaScript code itself.
  5. The parent PHP code is waiting for the JavaScript input, and does not continue until the prompt text has been entered.

But, I have not been able to figure out how to pass the JavaScript variable back to the parent PHP code.

What I have so far is very simple, but it is working exactly as I intended:

function getReprNotes() {
    ?>
        <script>
            var REPRNOTES = prompt('Please enter any appropriate reprocessing request notes');
            alert(REPRNOTES);
       </script>
    <?php
}        

getReprNotes()

Note that I want to pass the REPRNOTES text / variable back to the parent script.

Can anyone tell me how I need to do this, using the above code?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 5
    have you heard of AJAX ? –  Mar 06 '14 at 19:23
  • Your statement above that "The parent PHP code is waiting for the JavaScript input, and does not continue until the prompt text has been entered" is incorrect. By the time the browser parses the page, the PHP execution is done. You cannot interactively use JavaScript and PHP simultaneously. – elixenide Mar 06 '14 at 19:32
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – John Dvorak Mar 06 '14 at 19:35

3 Answers3

1

Keep in mind javascript is client side scripting, and php is server side. The only way for you to send information to PHP is by making a call to the server, the best way to do this from the client using javascript without submitting a form is by is using AJAX.

Take a look at these tutorials: 5 Ways to make AJAX calls with jquery and 24 best practices for AJAX implementations.

Onema
  • 7,331
  • 12
  • 66
  • 102
1

This is what AJAX calls are for. Split your PHP into two and have the javascript issue an ajax call which triggers the second. If you can use jQuery then you want to make a $.ajax() call: https://api.jquery.com/jQuery.ajax/

If you can't or don't want to use jQuery you can still make ajax calls through XMLHttpRequest objects: http://www.w3schools.com/Ajax/default.asp

  • W3schools is a terrible resource. Here, take this one: http://mdn.beonex.com/en/DOM/XMLHttpRequest/Using_XMLHttpRequest.html – John Dvorak Mar 06 '14 at 19:34
0
$.ajax({
    url: '/to-some-url',
    data: 'data=' + value,
    type: 'POST',
    success: function (response, textStatus, jqXHR) {
        if (jqXHR.status > 0) {
            // do something here
        }
    }
});

You can use jquery ajax method to pass javascript variable value to php by post(or get). But I don't understand what you really want to achieve

canon
  • 40,609
  • 10
  • 73
  • 97
Abhishek Patidar
  • 1,557
  • 1
  • 16
  • 25