[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Turning a Javascript cookie variable into a PHP variable

Posted on 2009-02-12
9
Medium Priority
?
661 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

0
Comment
Question by:BenthamLtd
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:BenthamLtd
ID: 23621027
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?
0
 
LVL 22

Accepted Solution

by:
Ivo Stoykov earned 1000 total points
ID: 23621040
> 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
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 1000 total points
ID: 23621142
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BenthamLtd
ID: 23622795
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

0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 23626141
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
0
 

Author Comment

by:BenthamLtd
ID: 23632940
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

0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 23633061
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...
0
 

Author Comment

by:BenthamLtd
ID: 23633083
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.
0
 
LVL 15

Expert Comment

by:fsze88
ID: 23633136
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question