1.11M Members

Pass Javascript variables to PHP functions?

 
0
 

Hello

How can I do something like this:

<script type="text/javascript">
var one="hi ";
var three="there";
<?php echo ConcatTwoStrings(?>one<?php,?>two<?php);?>;
</script>

That should produce "Hi there". Yes, I know there is concat Javascript functions; Thats not the point as I was simply showing a easy example.

How can I do the above?

Thank you

 
0
 

PHP is server Side script and Javascript client-side.So You can't do this.But you can use AJAX to call a PHP function from Javascript.

 
0
 

PHP is server Side script and Javascript client-side.So You can't do this.But you can use AJAX to call a PHP function from Javascript.

Great :) How do I do this?

I currently have this:

PHTML:

 <script type="text/javascript">
 $j(document).ready(function() 
                {



                    var variablecalle=$j("#listadetiendas").val();


                    var currentSessionID = "<?php echo session_id(); ?>";
                    var data='calle='+encodeURIComponent(variablecalle)+'&ses='+currentSessionID;



                    $j.ajax({
                            url: '../../updatesession.php',
                            type: 'POST',
                            data: data
                            }).done(function(data){


                            $j("#currSess").html(data);
                            $j("#currSess").html( $j("#currSess").text() );
                            var wrap = $j("#currSess");
                            var text = wrap.text();
                            wrap.replaceWith(text);

                            });
                });
                </script>

Now here is the problem: I cant access that PHP object/function in a AJAX call.

updatesession.php:

<?php

    require 'C:/folder/app/Mage.php';
    Mage::app();
    Mage::getSingleton('core/session')->setTest($_POST['calle']);


?>

When I access the getter, it shows null. I think its because the PHP object isnt passed so how could I pass it (and later return it)?

 
0
 
var currentSessionID = "<?php echo session_id(); ?>";
var currentSessionID = "<?php echo session_id[]; ?>";

change line number 10 to below line and you have to give some value in session index you are passing null value in currentSessionID.

 
0
 

PHP objects are not persistent. On ajax call, the server accepts info without any hangover - i.e. info from previous server actions. Session data is available though, but that's hardly the same as the data is usually in a file (or DB). You can set objects to be persistent, but that's a pretty big consideration. PHP is build up, tear down. So, without object persistence or storing the state of dead objects in sessions, you need to create all your objects from scratch.

 
0
 

i have a form based up on selection iam showing selected items in the page simultaneously.now i want POST data which is available in javascript variable in current file to another PHP file.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article