1,105,417 Community Members

Pass Javascript variables to PHP functions?

Member Avatar
riahc3
 
1,404 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
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

Member Avatar
Bachov Varghese
Posting Whiz in Training
205 posts since Sep 2011
Reputation Points: 45 [?]
Q&As Helped to Solve: 52 [?]
Skill Endorsements: 6 [?]
 
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.

Member Avatar
riahc3
 
1,404 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
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)?

Member Avatar
AARTI SHRIVAS
Posting Pro in Training
410 posts since Dec 2012
Reputation Points: 2 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 3 [?]
 
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.

Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
poosapatisuresh
Newbie Poster
1 post since Oct 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article