0

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?

3 Answers 3

0

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/

2
  • 1
    No, he knows it's 'AJAX', but what he's looking for is referred to as a callback. Commented Sep 13, 2012 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 ? Commented Sep 13, 2012 at 15:37
0

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.
0

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.

2
  • it doesnt work, everytime i get the session value it is the same thing... but when i refresh the page it change Commented Sep 13, 2012 at 15:43
  • Can you post your new code that's not getting different values on each request? Commented Sep 14, 2012 at 3:20

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.