0

My page has a mix of Javascript and php. Javascript is used to select a string from several choices. Then some php code needs to do something with this string.

I have seen some examples that seem to be talking about passing variables across different web pages. I'm just doing this all on one page and all I'm trying to pass is a string.

I guess another solution would be to use php to select the string, if that's possible?

5
  • All you want to do is to pass a string from pageA to pageB? You should try sending that string as a part of the URL or as a POST request, or in the worst case - cookies. Commented Mar 6, 2012 at 21:00
  • 1
    You probably get better answers if you explain what the string is for and what you want to do with it. Whether JS and PHP are on the same page does not matter. PHP is executed first, the result (text) is sent to the browser where it evaluates it as JavaScript and HTML. Commented Mar 6, 2012 at 21:02
  • The only way to pass a javascript variable to PHP is to send a POST or GET method to the php script, but this cannot be done without reloading the page. If you are doing this, you might as well just use PHP to take input from the form elemnts on your page <?php $example=$_POST['example_form_name'];. Otherwise you may need to describe your script in a little more detail. Commented Mar 6, 2012 at 21:02
  • 1
    JavaScript and PHP run in different contexts: PHP at your web server, and JavaScript at the browser. Communication between them involves a network, either HTTP or WebSockets. Without knowing the nature of what you want to do it's hard to comment beyond that. Commented Mar 6, 2012 at 21:02
  • I'm not trying to pass a string across pages. I'm trying to pass a string that was retrieved through Javascript to php in the same page. In the original code the string is hardcoded. I'm just adding Javascript so that the string can be dynamically changed at runtime. Commented Mar 6, 2012 at 22:21

3 Answers 3

4

By the time your browser renders the page, PHP has finished its job and closed the connection.

To pass some value determined on the client-side with js you need a new request. This means you need to use Ajax.

Sign up to request clarification or add additional context in comments.

1 Comment

When he says "You'll need to make a request to the server via JavaScript", he means AJAX.
2

PHP parses data server-side (before the page loads), whereas JavaScript handles everything browser-side (after or on page load). You can't pass JavaScript into PHP functionality unless you use AJAX to send the PHP script a variable:

JavaScript

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">

<script type="text/javascript">
   // Find whatever string you need
   var something = $('.input').val();

   $(function() {
      $.ajax({
         url: '/path/to/php_script.php',
         type: 'POST',
         data: 'variable='+something,
         success: function(data) {
            $('.display_div').html(data);
         }
      });
   });

</script>

PHP

$something = $_POST['variable'];

// Do something with it
echo strlen($something);

9 Comments

What is php_script.php? Is that the name of the page where all the code is located?
php_script.php is just a sample PHP file which I made up. You need to put all your PHP which modifies your string in this separate file
So in your example, that PHP code goes inside that other file?
A good information also is that the above example uses the jquery framework which you must include before it will work.
What if I don't even need to modify the string? Do I still need an external file? In the original code the url is hardcoded into the php. I'm just using Javascript so that that url can be dynamically changed at runtime.
|
1

You'll need to make a request to the server via JavaScript, since PHP is a server-side language. You could use a POST or GET variable to send the string to the same page and then add PHP code to check for that variable when the page is loaded.

The PHP would be something like:

if(isset($_POST['string'])) {
// do something with the string
}

2 Comments

This method doesn't use Ajax?
You could do it either way, a page reload or via AJAX. Although if you do it with AJAX you might want to separate the PHP code that handles the string just for clarity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.