We help IT Professionals succeed at work.

Turning a Javascript cookie variable into a PHP variable

Medium Priority
679 Views
Last Modified: 2012-06-27
Dear EE community,

This is a follow up to yesterday.

[1] User presses a shortcut key -> this brings up a Javascript prompt box -> user enters value into box -> this is then stored as a Javascript cookie....

I would then like to turn this Javascript cookie into a PHP variable so I can query my ODBC lookup database!

What is meant to happen next:

[2] .... results table is PHP echo'd if it matches the value of Javascript cookie (i.e. what the user inputted) -> user then selects the correct value they want from the echo'd list -> the following values are copied from the ODBC lookup database and fill in the HTML forms on the previous page: Sales Ledger No (this is the value they are searching for atm), Company Name, Contact Name, Telephone Number, Email, Postcode
<!-- new_sheet.php // this is the code for the shortcut key on the main form page... just so you can see what it's doing. This works nicely so no need to make any changes here -->
 
shortcut.add("F7", function() {
var lookup1 = prompt("Lookup Sales Ledger Ref", "");
createCookie('lookup1', lookup1, 7)
if (lookup1!=null) 
{
window.open('lookup_salesledger.php','mywindow','width=400,height=200'); 
}
	});
 
 
 
<!-- lookup_salesledger.php // this is the page I'm having a problem with -->
 
<script type="text/javascript">
document.write (readCookie('lookup1')); // test I used to see if the cookie is storing properly. Yes it is
</script>
 
/////////// Problem area here ///////////////////////////////
<script type="text/javascript">
<?php $salesref = ?>readCookie('lookup1');  // THIS BIT HERE IS THE PUPPY. Now obviously the code I've cobbled together will not work at all. Could you tell me a better way of doing it?
</script> 
///////////////////////////////////////////////////////////////////
 
<?php 
$sql_salesref = "SELECT * FROM division WHERE oprano = ". $salesref ." ";   // this is the ODBC lookup bit
$rs = odbc_exec($odbc_conn,$sql_salesref);
while (odbc_fetch_row($rs))
{
	$divname=odbc_result($rs,"divname");
}
odbc_close($odbc_conn);
 
 
?>

Open in new window

Comment
Watch Question

Author

Commented:
The other idea I just had was to maybe create a hidden form. Make the value of that, the Javascript cookie and then get PHP to $_SERVER[PHP_SELF] on the SQL query. Reckon that's a good approach?

So errr, maybe:

<form id="whatever" name="lookup1" value="javascript:(readCookie('lookup1'))" type="hidden">


Maybe, maybe, baby?
CERTIFIED EXPERT
Commented:
> I would then like to turn this Javascript cookie into a PHP variable
To do this you must callback the server sending JS value either as FORM or QueryString variable.
No other way.
HTH
i

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:
you can set cookie by javascript and read it by php scripting $_COOKIE["lookup1"]
<?php
$salesref = $_COOKIE["lookup1"]; at line 22
?>
I tested on IE6, FF3. they are works

more reference
http://www.w3schools.com/PHP/php_cookies.asp
http://hk.php.net/manual/ro/reserved.variables.cookies.php
http://hk.php.net/setcookie

Author

Commented:
That works brilliantly, thank you fsze88!!

Ok then, to save me having to post another question, here is my new code for the PHP ODBC on lookup_salesledger.php. We are now covering part [2] as mentioned on my first post.

The forms I would like to get the results punted back into are simple text forms. These are on the previous page (new_sheet.php).

<form id="fSalesRef" name="fSalesRef" method="post" action="">
                  <input name="fSalesRef" type="text" />

<form id="fCompany" name="fCompany" method="post" action="">
                  <input type="text" name="fCompany" />

<form id="fContact" name="fContact" method="post" action="">
                  <input type="text" name="fContact" />

<form id="fPostcode" name="fPostcode" method="post" action="">
                  <input type="text" name="fPostcode" />

<form id="fEmail" name="fEmail" method="post" action="">
                  <input type="text" name="fEmail" />

Obviously I'd have to do something with "value=" in the forms. What would be the best way to pull back the lookup data into these forms on the other page? Maybe abuse PHP $_SESSION or JavaScript sessions?

Thank you ever so much for all your help so far. You've just saved me hours upon hours of hair pulling.

Regards,
> Luke
<?php 
$salesref = $_COOKIE["lookup1"];
 
$sql_salesref = "select division.divno AS divno, division.compno AS compno, division.divname AS divname, company.compname AS compname, division.oprano AS oprano, address.address1 AS address1, address.address2 AS address2, address.address3 AS address3, address.postcode AS postcode, contact.surname AS surname, contact.forename AS forename, contact.primephone AS primephone, contact.email AS email from (((company join division on((company.compno = division.compno))) join contact on((division.divno = contact.divno))) join address on((contact.addrno = address.addrno))) WHERE oprano = '$salesref' ";
$rs = odbc_exec($odbc_conn,$sql_salesref);
echo "<table cellpadding=\"4\">";
echo 	"<tr>";
echo 	"<th align=\"left\">oprano</th>";
echo 	"<th align=\"left\">divname</th>";
while (odbc_fetch_row($rs))
{
	$oprano=odbc_result($rs,"oprano");
	$divname=odbc_result($rs,"divname");
	echo "<tr><td>$oprano</td>";
	echo "<td>$divname</td>";
}
odbc_close($odbc_conn);
echo "</table>";
?>

Open in new window

CERTIFIED EXPERT

Commented:
you cannot post all the forms, but only one.
generally speaking I cannot see any particular reason to use many forms.
if in any case you must you should consolidate your data with JS and store them into a (hidden for example) field of the submitted form.
than you'll have it into you php $_FORM variable

HTH

i

Author

Commented:
Ok so I've thrown together your idea, coupled with some code taken from another site. "How to populate fields from new windows using JavaScript".

Can't seem to get the returned lookup value to update the form on the previous page. Any ideas?
<!-- lookup_salesledger.php -->
 
<script type="text/javascript"><!--
      function input(formName, obj, val){
         opener.document.forms[formName].elements[obj].value = val;
         self.close();
      }
      function select(formName, obj, idx){
         opener.document.forms[formName].elements[obj].selectedIndex = idx;
         self.close();
      }
      function checkRadio(formName, obj, choice){
         opener.document.forms[formName].elements[obj][choice].checked = true;
         self.close();
      }
      function check(formName, obj, choice){
         opener.document.forms[formName].elements[obj].checked = choice;
         self.close();
      }
    -->  
</script>
 
<script type="text/javascript">
<!--
document.write ('<form onsubmit="return false">');
document.write ('<label><input type="text" name="fSales_L" value="<?php echo $oprano ?>"></'+'label>');
document.write ('<input type="button" onclick="input(\'sheet\', \'fSalesRef1\', this.form.fSalesRef_L.value)" value="Update">');
document.write ('</'+'form>');
//-->
</script>
 
 
<!-- new_sheet.php -->
// This is where the Sales Ledger Ref field is located. The value from lookup_salesledger.php form needs to go here.
 
<form action="insert_sheet.php" name="sheet" method="post" onsubmit="javascript:return confirm('Add this sheet?');">
 
// rest of form input go here..... //
 
<label><form id="fSalesRef" name="fSalesRef" method="post" action="">
<input name="fSalesRef1" value="" type="text" /></label>   // LOOKUP VALUE NEEDS TO GO INTO THIS FORM!

Open in new window

CERTIFIED EXPERT

Commented:
document.write destroys anything already existing before calling this function.
What actually do you want to achieve? Because this is already far away from your original question...

Author

Commented:
Don't worry, I've just got it working. It was me not having my forms named properly lol. Schoolboy error...... *shakes head*

Points being allocated accordingly.

Thanks to everyone for all the help.
CERTIFIED EXPERT

Commented:
You means get back the value of cookie by javascript?

how about using getCookie function by http://www.w3schools.com/JS/js_cookies.asp  ?
I checked IE6,FF3. they works
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.