Current location: Hot Scripts Forums » Programming Languages » PHP » PHP and JavaScript problem


PHP and JavaScript problem

Reply
  #1 (permalink)  
Old 04-19-06, 03:03 PM
twome twome is offline
New Member
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Post PHP and JavaScript problem

Okay, I have 2 drop down list's. One for city one for state. When the user clicks (and makes a selection) for a State the onchange='populateCity()' function is called. The function is part of an included file called at the top of the page:
<script language="JavaScript" src="common/city_dropdown.js"></script>

Now here's my problem:
When a user enters information, the FORM will post the information, check if the user name already exists.... if the user name DOES already exist, all the FORM information gets put into session including city and state:

$_SESSION['city']=trim($_POST['city']);
$_SESSION['state']=trim($_POST['state']);

...then redirects back to the FORM page where all fields are populated with the data the user entered, except for the username and password. Thus telling the user: the username is taken, please select a new username.

However, the city and state fields are not being populated with the user data when this happens. I think my problem is passing the session information into javascript... so that then the "city_dropdown.js" include file can populate the city/state drop down lists.

At the top of "city_dropdown.js" I do this for a check:

var postCity = '<?= $_SESSION["city"] ?>';
var postState = '<?= $_SESSION["state"] ?>';

function populateState(defaultState) {
if ( postState != '' ) {
defaultState = postState;
}
..... other code
}
Reply With Quote
  #2 (permalink)  
Old 04-19-06, 06:43 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
You can't use php variables in an external javascript file.

You should either write the javascript inline in the php file so you have access to the php variables OR just write a new javascript function for the body.onload event and pass it the appropriate values.

PHP Code:

<body onload="myFunction('<?= $_SESSION["city"?>', '<?= $_SESSION["state"?>');">
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Javascript Variable to PHP watadude PHP 4 01-15-11 06:44 PM
PHP and JavaScript causing a problem of storing the value phplearner PHP 4 07-23-05 11:15 PM
JavaScript with PHP phplearner PHP 2 07-16-05 11:46 AM
javascript multiple select menu for php? isaacmlee JavaScript 1 10-15-04 09:53 AM
Javascript vs PHP, problem with arrays Chuff JavaScript 0 10-03-03 04:01 AM


All times are GMT -5. The time now is 07:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.