28rain Posted September 2, 2009 Share Posted September 2, 2009 I have a list of names in a javascript file, and I want the names to be inputted by PHP... This is the javascript: function StateSuggestions() { this.states = [ "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" ]; } And I want to make a list of names similar using: while ($row = mysql_fetch_array($result)) { $name=stripslashes($row['name']); $display_block.="\"$name\", "; } Excetra excetra and i want $display_block to be echo'd into that javascript file... Any help please (Oh and i have never used javascript before!) Thanks Link to comment https://forums.phpfreaks.com/topic/172854-solved-mixing-php-and-javascript/ Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 $display = "<script type='text/javascript'>\n"; $display .= "jsArray = new Array();\n"; $count = 0; while ($row = mysql_fetch_array($result)) { $name=stripslashes($row['name']); $display .= "jsArray[".$count."] = '".$name."';\n"; $count++; } $display.= "</script>"; echo $display; Link to comment https://forums.phpfreaks.com/topic/172854-solved-mixing-php-and-javascript/#findComment-911011 Share on other sites More sharing options...
Psycho Posted September 2, 2009 Share Posted September 2, 2009 PHP code while ($row = mysql_fetch_array($result)) { $states[] = '"'.stripslashes($row['name']).'"'; } $jsStateList = implode(',', $states) Where you create the js code function StateSuggestions() { this.states = [ <?php echo wordwrap($jsStateList);?> ]; } Link to comment https://forums.phpfreaks.com/topic/172854-solved-mixing-php-and-javascript/#findComment-911030 Share on other sites More sharing options...
28rain Posted September 2, 2009 Author Share Posted September 2, 2009 LOL soo basic, but thanks for the help, I never would have got there without you! Thanks Link to comment https://forums.phpfreaks.com/topic/172854-solved-mixing-php-and-javascript/#findComment-911042 Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 oh, it would just make the javascript look like jsArray[0] = 'value'; jsArray[1] = 'value1'; #etc but mjdamato's example is probably a little cleaner. Link to comment https://forums.phpfreaks.com/topic/172854-solved-mixing-php-and-javascript/#findComment-911043 Share on other sites More sharing options...
Psycho Posted September 2, 2009 Share Posted September 2, 2009 On a second look, stripslashes() will un-escape single quotes in the content. But, if the content has a double quote your javascript will fail (becuase the js elements are defined with double quotes). I would suggest not using strpslashes and defining the javascript elements with single quotes - that way the escaped single quotes will be needed: PHP code while ($row = mysql_fetch_array($result)) { $states[] = "'{$row['name']}'"; } $jsStateList = implode(',', $states) Link to comment https://forums.phpfreaks.com/topic/172854-solved-mixing-php-and-javascript/#findComment-911050 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.