Link to home
Start Free TrialLog in
Avatar of Ivan Golubar
Ivan Golubar

asked on

Sending variable from Js to php

As title is saying i am trying to send variable from js to php. But no success: I am getting "1" as echo.
 Can You tel me from the code i am providing here what is wrong?


function saveToProjectsListF(){
 
$.ajax({
  type: 'POST',
  url:'/wp-content/themes/net4/user.php',
  data: {"actualproject": weGotTheuserHahaHa+"123"},
});
   
}

Open in new window

     
<?php
if(isset($_POST["actualproject"])){
	$actualproject = $_POST["actualproject"];
}else{
    	$actualproject="1";
}
?>

Open in new window


 <p><?php echo $actualproject; ?></p>  

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Can't see anything obvious wrong with your code, but I'm guessing you're only showing part of it.

You say you're only seeing "1" as the echo, but nothing in your code shows how you're getting that. You're not handling the response in your AJAX call, so where exactly are you getting the "1" displayed?
Avatar of Ivan Golubar
Ivan Golubar

ASKER

I don't need  response back to ajax. I need to process  variable then in an another.php page.

I have "select" html element which fires function on change event (first code), to send an variable to an another.php. And  then  with an link I am migrating to that another.php when is needed from header.php. And i would like to see that variable, but i see "1".  (check other my code)
OK. Can't really follow your logic. Do you have a link to your page so we can see it live. You may also want to post up the full code for the 2 pages user.php and the one with the AJAX call on.
My project tree:

project_folder
     js_folder 
       JS_file.js
     sub_folder
       header.php
       another.php

Open in new window



In header.php i have html  with "select" element.  On change event of "select" , code in JS_file uses function "saveToProjectsListF()"
to send variable to another.php.
And on header.php i have a link to another.php.  When i need then, i am going to my another.php, and there i need to see my "$actualproject" variable.


Maybe You can show me an yours example how you are sanding an variable to an php page.
OK. I think I can see what your problem is.

When you change the <select> the actualproject variable is POSTed to the user.php page via AJAX. That's it! It doesn't keep that variable in that page for later use. If you then click on a link to user.php page, then the variable won't be available because you haven't POSTed it to the page - you've just visited the page with a normal link. The variable that you'd previously POSTed has been lost.

If you want to keep thr variable for later use then you would need to store it somewhere when you first POST it. Probably the simplest solution is to store it in a session variable.
I have now
<?php
  $actualproject = $_POST["actualproject"];
  session_start();
  $_SESSION["actualproject"] = $actualproject;
?>

Open in new window


Then in
<body>
.....
......
<p><?php echo $_SESSION["actualproject"]; ?></p>  
.....
......
</body>

Open in new window

And i am not getting anything.

What i am doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Success. Thank you.
 I see now the wanted variable. After doing final edits
"checks to see whether or you're not you're POSTing to the file"
I did move yours code to the very top of everything. Why it was not working ,when the code  was
lover-under some other code will stay question for now. But maybe i should take it as a fact and don't think about it any more.