Link to home
Start Free TrialLog in
Avatar of BenthamLtd
BenthamLtd

asked on

Turning a Javascript cookie variable into a PHP variable

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

Avatar of BenthamLtd
BenthamLtd

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Ivo Stoykov
Ivo Stoykov
Flag of Bulgaria 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
SOLUTION
Avatar of Kin Fat SZE
Kin Fat SZE
Flag of Hong Kong 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
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

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

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