Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been looking for hours, I know javascript is the client side and php is the server side and for communicating they need to use POST or whatever.

I have made this code in javascript first to retrieve the value i want and send it to php with jquery.

function getGroupName(test){

    var groupName = ($(test).parent().attr('id'));           

    $.post("setGroup.php",{ groupName:groupName } ,function(data) {

        if(data == 'yes'){          
            <?php   
                $testing = $_SESSION['currentGroup'];       
                $tsql2 = "select emailName,email from privacyEmails where tagID = '$tagid' and userEmail = '$testmail' and circleName = '$testing'";  

                $stmt2 = sqlsrv_query( $conn, $tsql2);  

                $order = 0;
                $emailNameT = "";
                $NameT = "";

                while( $row = sqlsrv_fetch_array( $stmt2,   SQLSRV_FETCH_NUMERIC))  
                {
                    $emailNameT = $row[0];
                    $NameT = $row[1];
            ?>
            makeEditPeople('<?php echo $NameT ?>','<?php echo $emailNameT ?>','<?php echo $order ?>');
            <?php
                $order = $order +1;
                }
            ?>
        }    
    });
}

this is the php code (setGroup.php) to get the $testing :

<?php
    include "connectionString.php";

    session_start();

    $groupName = $_POST['groupName'];

    $_SESSION['currentGroup'] = $groupName;

    echo "yes";    
?>

Please note that the makeEditPeople() in the js is a method that append the user received in parameter into a table which works well.

My problem is : I want to send a javascript value (groupName) to SESSION value in php throught jquery $Post, then in the same time, I want to retrieve it without refreshing the page. Is that possible?

share|improve this question

3 Answers

The issue is your "if (data == 'yes')" Javascript command cannot control whether or not inline PHP get executed; the contents there will run regardless, since it's another language all together.

The better solution is to modify setGroup.php to not return just 'yes' but both set and return the value:

<?php
include "connectionString.php";

session_start();

if (isset($_POST['groupName']))
  $_SESSION['currentGroup'] = $_POST['groupName'];

echo $_SESSION['currentGroup'];
?>

Now if you POST to setGroup.php with no groupName set, it won't update the session, but will return the current value (data in the javascript function). If you do set it, it will return back the new value to Javascript.

share|improve this answer
it doesnt work, everytime i get the session value it is the same thing... but when i refresh the page it change – hugo Sep 13 '12 at 15:43
Can you post your new code that's not getting different values on each request? – MidnightLightning Sep 14 '12 at 3:20

If you are refreshing the page after you click on a link to activate and AJAX action, then this means one of two things:

  1. You have an error in javascript (so the link ends up getting followed)
  2. You haven't told javascript to not refresh the page.

You probably have and "onclick" or (if using jQuery "correctly") then you have $("#id").click( function(e) {} ) handler*. Make sure these "return false;" at the end

<a href="#" onclick="RunSomeFunction();">Click</a>

function RunSomeFunction() {
    // Do Stuff
    return false
}

or, using jQuery

<a href="#" id="RunMyFunction">Click</a>

$("#RunMyFunction").click( function(e) {
   e.preventDefault();
   // Do Stuff
   return false;
)}

Notes:

  1. You can also use other binding functions, not just click(). But click is easier to read
  2. e.preventDefault is enough, you don't need that and return false; just giving you two options.
share|improve this answer

What you are looking for is called AJAX, it's a JavaScript "feature" that allows you to make requests to the server without refreshing the browser.

Since you are using jQuery it couldn't be simpler:

$.post (
  'http://example.com/your.php', // URL of your script
  {
    Param1: 'value',
    Param2: 123
  },
  function ( response ) {
    // request holds whatever the server returned
  }
);

See this for more info: http://api.jquery.com/jQuery.post/

share|improve this answer
1  
No, he knows it's 'AJAX', but what he's looking for is referred to as a callback. – Ohgodwhy Sep 13 '12 at 6:25
i just need to retrieve the groupName to php so maybe this code work what you mean but request hold whatever the server returned? $_REQUEST['Param1'] will show value ? – hugo Sep 13 '12 at 15:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.