JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 28th, 2001, 11:45 AM
birwin birwin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Location: Vancouver, Canada
Posts: 29 birwin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 7 m 25 sec
Reputation Power: 0
JavaScript to PHP (or mySQL)

I am using a quiz program coded in JavaScript. I include it in one of my PHP pages. It writes the score of the test to a flat file in the cgi-bin.

Is there a way to take data created by JavaScript and either manipulate it with PHP or dump it directly into a mySQL database?

Brian

Reply With Quote
  #2  
Old May 28th, 2001, 12:31 PM
jerome jerome is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: Seattle
Posts: 5 jerome User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jerome Send a message via AIM to jerome Send a message via Yahoo to jerome
Use the redirect thingy

strAddress= "http://www.yourpage.com/info.phpblah=strInfo&blah2=strInfo2"

where strInfo/strInfo2 are the variables you need to pass

locaction.href = strAddreses


try that, I dunno if It'll work, but it might.

Reply With Quote
  #3  
Old May 28th, 2001, 05:40 PM
Adrian2 Adrian2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Location: London, England
Posts: 251 Adrian2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 14
I'm not entirely clear what you're trying to do. However, I would assume your quiz script involves submitting a form. If so, you can use JavaScript to setup whatever variables you need with an onSubmit handler, then use a PHP script to process the result. If necessary, create empty type="hidden" form fields to store your values.

PHP can only pass variables when requesting a script, so unless you're linking to a script and can pass variables in the URL as Jerome suggested or you're submitting a form, then PHP can't help you. But you're probably doing one of these, so no problem.
__________________
Please don't visit my lame personal website, www.webhamster.co.uk. Half the time it doesn't even work!

Reply With Quote
  #4  
Old May 28th, 2001, 08:22 PM
biss biss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Melbourne, Australia
Posts: 5 biss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 44 sec
Reputation Power: 0
Send a message via ICQ to biss
Following your suggestion the following should work no problem:

function deleteProduct(form)
{
var ID = form.prodID.value;
if(confirm("Do you really want to delete this product?"))
window.location.href("admin_prod.php?delete=1&prodID=ID");
else
{
alert("Thought so 8)" + ID);//testing if ID works
}
}

However, it work OK within the JavaScript function where alert window shows "Thought so 8) 111969" but PHP gets only 'ID'
(here is an extract from an error message):

"DELETE FROM Products WHERE ProductID=ID"

Is there anything else to it?

Regards

biss

Reply With Quote
  #5  
Old May 29th, 2001, 01:26 PM
birwin birwin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Location: Vancouver, Canada
Posts: 29 birwin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 7 m 25 sec
Reputation Power: 0
PHP to Java??

Thank you! Thank you!

I really appreciate this help. I was able to get the result I needed.

Now that I can move a variable from Java to PHP is there a way to move a variable from PHP to Java? The data that I need to process would be in the url.

Brian

Reply With Quote
  #6  
Old May 29th, 2001, 01:44 PM
Atrus's Avatar
Atrus Atrus is offline
yet another member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 262 Atrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 52 sec
Reputation Power: 13
Hi!

Is your JS in an external file? If not, you can do the same thing (using url data) the other way around.

JS reads from the url with the object location.search

Code:
         var input = unescape(location.search.substring(1))
         if (input) {
           // Divide long string into array of name/value pairs.
           var srchArray = input.split("&")
           var tempArray = new Array();
           for (i = 0; i < srchArray.length; i++) {
             // Divide each name/value pair temporarily into a two-entry array.
             tempArray = srchArray[i].split("=");
             // Use temp array values as index identifier and value.
             results[tempArray[0]] = tempArray[1];
           }
         }
I myself took this from 'The Javascript Apostle'. Thanks to them!!

A different approach for this would be making php set a cookie (void is shooting me for this ) containing the data and have JS read from that cookie then. This would even work without reloading the JS page if it stayed in a diffrent frame in the meantime....
I'm not recommending the use of cookies, though... people might not allow cookies and your gimmicks will look broken in their browsers.


Greetings,

Atrus.

Reply With Quote
  #7  
Old May 29th, 2001, 09:06 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
It is exceedingly easy to get a variable from PHP to javascript:

<script language="Javascript">
jsvariable = "<?php echo $phpvariable; ?>";
</script>


Remember, though, if the PHP variable is a string with line breaks, you need to escape that string, since a javascript string can only be on one line.

example:
<?php

$phpvariable1 = "Hello, this string has\n two lines";
$phpvariable2 = "This one
has
3 lines";

$phpvariable3 = "Hello, this string will \\n escape line breaks \\n in PHP so that Javascript can handle it.";
$phpvariable4 = addcslashes($phpvariable1, "\n\r");

?>
<html>
<head>
<script language="Javascript">

jsvariable1 = "<?php echo $phpvariable1; ?>";
jsvariable2 = "<?php echo $phpvariable2; ?>";
jsvariable3 = "<?php echo $phpvariable3; ?>";
jsvariable4 = "<?php echo $phpvariable4; ?>";

</script>
</head>
</html>

The above script would work for jsvariable3 and jsvariable4, but not for 1 and 2. The output to the browser would be:

<html>
<head>
<script language="Javascript">

jsvariable1 = "Hello, this string has
two lines";
jsvariable2 = "This one
has
3 lines";
jsvariable3 = "Hello, this string will \n escape line breaks \n in PHP so that Javascript can handle it.";
jsvariable4 = "Hello, this string has\n two lines";

</script>
</head>
</html>

If you want a more sophisticated way to send variables back and forth between Javascript and PHP, you could take a look at the WDDX functions, which are supported by PHP, and which provide a way to serialize variables and arrays from one environment to another. Using WDDX, you should be able to transport a whole array from PHP to Javascript and vice-versa.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > JavaScript to PHP (or mySQL)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap