-1

I am struggling to get the following code to work. I want to take input from a javascript prompt, and output it using a php echo. When I try this I get an echo with the random numbers "12313" for no apparent reason. This is my code:

echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>'
   , '<script type="text/javascript">'
   , 'var code = prompt("Enter verification code", "");'
   , 'var getcode = code;'
   , '$.post("wp-members-register.php", { code: getcode }); </script>';

     $buffer_data['code'] = $_POST['code'];
    echo $buffer_data['code'];

I am very new to php so please bear with me. Perhaps I am not correctly posting the 'code' variable?

EDIT: Maybe somebody can show me a better way of inputting a text string and getting it in the php code to follow? Note I am working with the wp-members wordpress plugin in the wp-members-register.php file.

barnacle.m
  • 2,070
  • 3
  • 38
  • 82
  • Is the code we're looking at on `wp-members-register.php`? What are you trying to do exactly? As in, *why*, not how. – Will Feb 05 '14 at 12:53
  • I am trying to get a value from a javascript prompt before continuing with the rest of the code. What I am ultimately trying to do is create a verification system, within the wp-members plugin. – barnacle.m Feb 05 '14 at 12:55
  • you need ajax, if you want to pass a variable from js to PHP, as php is server side, it's ran on the server and sends the output (result) to the user, while the js is ran on the client side, after the page loads (after the php already processed the page and sent the final output) – Arrok Feb 05 '14 at 13:13

2 Answers2

1

Outputting javascript using PHP doesn't make any sense as javascript is a client side scripting which is executed after loading the DOM elements. PHP code is triggered when the request is sent from the browser to the server.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
0

PHP is called before the JavaScript and cannot be called after.

You have to do everything you have to do in PHP before you use any JavaScript.

On thing you can do is use AJAX.

<?php
//php code here before the javascript
?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var code = prompt("Enter verification code", "");
$.get( "wp-members-register.php?code=code", function( data ) {
   document.write("Data Loaded: " + data);
});
</script>
bug
  • 342
  • 1
  • 10