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 July 4th, 2001, 04:21 PM
phpnewbie3 phpnewbie3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 49 phpnewbie3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Question passing var from javascript to PHP??

Is it possible to pass a variable such as an argument at the end of a URL to PHP from javascript???

Here is what I need to do:
I am using the <SCRIPT LANGUAGE="JavaScript" SRC="populate.js"></SCRIPT> on a regular HTML page. On the populate.js I want to hit a database using PHP and grab some info then write that info into javascript format as an array. That array will then be used to populate the javascript on the regular HTML page.

So what I am trying to do is pass a variable from the regular html page to the populate.js so that I will be able to use that variable to hit the database and get the necessary info and then use that necessary info to populate the javascript array. (taking a deep breath )

Any help will be appreciated.

Vince

Last edited by phpnewbie3 : July 4th, 2001 at 04:25 PM.

Reply With Quote
  #2  
Old July 4th, 2001, 04:41 PM
riv's Avatar
riv riv is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 465 riv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 46 m 15 sec
Reputation Power: 13
ho well

You can always try to create hidden forms and set their values with js. Perhaps that if you create, let's say, one text box you could append the variables the same way it does with the GET-method URL style and use str_parse(), *I think*, to get the vars once your inside PHP.

Reply With Quote
  #3  
Old July 4th, 2001, 05:24 PM
Zitan Zitan is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2001
Location: New Zealand
Posts: 638 Zitan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 10 sec
Reputation Power: 13
Greetings Newbie...

Quote:
I am using the <SCRIPT LANGUAGE="JavaScript" SRC="populate.js"></SCRIPT> on a regular HTML page. On the populate.js I want to hit a database using PHP and grab some info then write that info into javascript format as an array. That array will then be used to populate the javascript on the regular HTML page.


Yeah I've done some stuff with this and a bunch of cool dudes helped me from Devshed. Okay one thing you could do is this:

populate.js:
Quote:
document.write("<SCR" + "IPT SRC='populate.php' type='text/javascript'> <\/SCR" + "IPT>");


And then populate.php is your databasing code.

This should do the trick. You were always going to have to call a PHP page because otherwise the server wouldn't know what to do with it. There's more you can do with this combo, but give that a try first. Remember JS evaulates client side and PHP server side so you have to be careful when using them together.

Hope this helped,
Zitan.

Reply With Quote
  #4  
Old July 4th, 2001, 05:31 PM
Zitan Zitan is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2001
Location: New Zealand
Posts: 638 Zitan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 10 sec
Reputation Power: 13
Vince,

Quote:
So what I am trying to do is pass a variable from the regular html page to the populate.js so that I will be able to use that variable to hit the database and get the necessary info and then use that necessary info to populate the javascript array.


You can pass PHP variables back into Javascript with a echo... populate.js(<?echo("$var_to_Pass)";?>. But once again be careful because you'll find unusal things happen because the server does all the PHP first, and then does the JS when its gets to the browser.

Z.

Reply With Quote
  #5  
Old July 4th, 2001, 05:52 PM
phpnewbie3 phpnewbie3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 49 phpnewbie3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Hi Zitan,
Thanks for your reply. Here is what I did:

On the regular HTML page, I have this code:
<SCRIPT LANGUAGE="JavaScript" SRC="populate.js"></SCRIPT>

On the populate.js, I placed your code:
<SCRIPT LANGUAGE="JavaScript1.2">
<!--
document.write("<SCR" + "IPT SRC='populate.php' type='text/javascript'> <\/SCR" + "IPT>");

// -->
</SCRIPT>


and on the populate.php I have it echo this javascript code by hard coding the CustID when I hit the database, I will worry about passing the variable in later:

<script language="javascript">
<!-- Hide from browsers
var ar = new Array();
ar[0] = "Get a <a href='http://www.seesantabarbara.com'>SeeSantaBarbara</a> business page today and receive an enhanced listing for just $9.95 per month.<br>";// end hiding -->
</script>

I need to get the Array "ar" from the populate.php page back to the regular html page. but it doesn't seem to be working. I am receiving the javascript error "ar is undefined" on the regular HTML page.

Any help will be appreciated.

Vince

Reply With Quote
  #6  
Old July 4th, 2001, 06:06 PM
Zitan Zitan is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2001
Location: New Zealand
Posts: 638 Zitan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 10 sec
Reputation Power: 13
Okay Vince,

You can't pass variables from javascript to PHP, because the PHP is all finished before the JS even starts (assuming var ar is defined in populate.php). You can however pass the Javascript Array into PHP.. and then Output from PHP to Html. Could you do the array stuff in PHP as well? If the information you populating the array with is from database, you don't even need JS here... just output straight from PHP to HTML... but I could be misunderstanding the problem ....

If you tell me a little more about what the purpose of Javascript is here I might be able to help more, or not because I'm still just a newbie myself !

Z.

Reply With Quote
  #7  
Old July 4th, 2001, 06:21 PM
phpnewbie3 phpnewbie3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 49 phpnewbie3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
Unfortunetly, I am limited to what I can say because my client has me under a strict confidentiality agreement.
I can tell you this: the regular HTML page will be located on many different web sites(affiliate sites), all the other pages will be located on my clients server including populate.js and populate.php.

The regular html pages will have to hit my clients server in order to populate the content on their pages. It would be similar to a news web feed to a site. Where my client is providing the news and the regular HTML pages will be displaying the news. But the news will be different on each site based on their preferences.

This is the first time I have used javascript with PHP and it is killing me. Here is one client I should have turned down.

Vince

Reply With Quote
  #8  
Old July 4th, 2001, 06:34 PM
Zitan Zitan is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2001
Location: New Zealand
Posts: 638 Zitan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 10 sec
Reputation Power: 13
Hey Vince,

All good man... yeah I did it to myself I though hey it would be cool if I did this with Javascript and then used PHP for this... it can be a nightmare but once I understand what happened when it made things a lot easier.

Okay back to it: I still don't know why you need Javascript. You have the html forms on other sites which all loadup to your client's server. Why are you using that JS array, what we need to do is think of a way around that. Are you using MySQL and Html forms with this solution?

If your really tied to the JS array you could load the javascript array into a hidden form in propogate.js which would then load into propogate.php. However I'd still recommend using PHP for the array stuff if you can.

Keep it coming,
Z.

Reply With Quote
  #9  
Old July 4th, 2001, 06:54 PM
phpnewbie3 phpnewbie3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 49 phpnewbie3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 13
I still don't know why you need Javascript. You have the html forms on other sites which all loadup to your client's server.

************************************************

My client doesn't want the information to be displayed in html form textboxes, he wants it on the page like regular text, that is why I am using javascript to write the content directly to the other sites browsers.
Unless I am missing something here. Is it possible to write dynamic content to another website without using javascript?

Vince

Reply With Quote
  #10  
Old July 4th, 2001, 07:58 PM
Zitan Zitan is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2001
Location: New Zealand
Posts: 638 Zitan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 10 sec
Reputation Power: 13
Quote:
Is it possible to write dynamic content to another website without using javascript?

Greetings Vince,

Sorry but I haven't needed to do that before so maybe someone else can answer this question. Sorry to waste your time on that one.

So are you okay getting information from PHP to Javascript (echos and such) its just getting information into PHP from Javascript right?

The last problem was that the "ar" array couldn't load variables into PHP. You could do this by loading the array into a Hidden html form and submitting this to the PHP page. Also the Propogate.js could call the php page with some vars on it like propogate.php?ar[1]&ar[2] but I don't think that's real efficient. Where does your array data come from?

Z.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > passing var from javascript to PHP??

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