PHP - Is It Possible To Put A Php Variable Into A Javascript Variable
Hi i have this scroolbar javascript where you implement the text inside the java variable but because i want the data to come from the database i wanted to use a php example <? print($marker); ?> so i was wondring if it is possible to add a php variable into a javascript like this my javascript
at moment the javascript variable is like this Code: [Select] var myMainMessage=" Try this out! The message just keeps repeating. Use it for announcements, news and other items! ";i want to change to this Code: [Select] var myMainMessage="<? print($marker); ?>";is it possible if so can someone show me an example thanks the whole javascript Code: [Select] <SCRIPT LANGUAGE="JavaScript"> <!-- This script and many more are available free online at --> <!-- The JavaScript Source!! http://javascript.internet.com --> <!-- (c) http://www.wyka-warzecha.com --> <!-- Begin // THESE VARIABLES where i want to add the php // var myMainMessage=" Try this out! The message just keeps repeating. Use it for announcements, news and other items! "; var speed=150; var scrollingRegion=50; // END CHANGEABLE VARIABLES // var startPosition=0; function mainTextScroller() { var mainMessage=myMainMessage; var tempLoc=(scrollingRegion*3/mainMessage.length)+1; if (tempLoc<1) {tempLoc=1} var counter; for(counter=0;counter<=tempLoc;counter++) mainMessage+=mainMessage; document.mainForm.mainTextScroller.value=mainMessage.substring(startPosition,startPosition+scrollingRegion); startPosition++; if(startPosition>scrollingRegion) startPosition=0; setTimeout("mainTextScroller()",speed); } // End --> </script> Similar TutorialsI am trying to allow the user to update a variable he chooses by radio buttons, which they will then input text into a box, and submit, to change some attributes. I really need some help here. It works just fine until I add the second layer of variables on top of it, and I can't find the answer to this question anywhere. <?PHP require('connect.php'); ?> <form action ='' method='post'> <select name="id"> <?php $extract = mysql_query("SELECT * FROM cars"); while($row=mysql_fetch_assoc($extract)){ $id = $row['id']; $make= $row['make']; $model= $row['model']; $year= $row['year']; $color= $row['color']; echo "<option value=$id>$color $year $make $model</option> ";}?> </select> Which attribute would you like to change?<br /> <input type="radio" name="getchanged" value="make"/>Make<br /> <input type="radio" name="getchanged" value="model"/>Model<br /> <input type="radio" name="getchanged" value="year" />Year<br /> <input type="radio" name="getchanged" value="color" />Color<br /><br /> <br /><input type='text' value='' name='tochange'> <input type='submit' value='Change' name='submit'> </form> //This is where I need help... <?PHP if(isset($_POST['submit'])&&($_POST['tochange'])){ mysql_query(" UPDATE cars SET '$_POST[getchanged]'='$_POST[tochange]' where id = '$_POST[id]' ");}?> Hello all. I am very new to PHP, and I am not sure where to look or what I'm looking for in my current assignment. My task is to take in two numbers between 0-100. Once I take in that number, it should state beside it "The __ was accepted." The program should not accept any numbers greater than 100 or any characters. Once I do this, I must take a second number and do a similar thing. Finally, I must have a statement show up at the bottom stating which number is greater. Essentially, I need help in determining what I should use to place parameters, and how I can keep the program from echo ing any statement until input has been taken and tested for parameters. Any help you can provide will be greatly appreciated! I have just re-installed Xampp and suddenly my sites are now displaying lots of: Notice: Use of undefined constant name - assumed 'name' in ... Notice: Use of undefined constant price - assumed 'price' in ... this is an example of the line its refering too: $defineProducts[1001] = array(name=>'This is a product', price=>123); My login script stores the user's login name as $_SESSION[ 'name'] on login. For some unapparent reason, i'm getting errors stating that $user and $priv are undefined variables, though I've attempted to define $user as being equal to $_SESSION['name'], using $user to look up the the user's privilege level (stored as the su column ) in the SQL table, and then where the result of the sql query is $priv which is then evaluated in an if statement. I can't seem to figure out why this might not be working. The code I'm using: <?php session_start(); function verify() { //verify that the user is logged in via the login page. Session_start has already been called. if (!isset($_SESSION['loggedin'])) { header('Location: /index.html'); exit; } //if user is logged in, we then lookup necessary privleges. $_SESSION['name'] was written with the login name upon login. Privleges // are written in db as a single-digit integer of of 0 for users, 1 for administrators, and 2 for special users. $user === $_SESSION['name']; //Connect to Databse $link = mysqli_connect("127.0.0.1", "database user", "password", "database"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } //SQL Statement to lookup privlege information. if ($result = mysqli_query($link, "SELECT su FROM accounts WHERE username = $user", MYSQLI_STORE_RESULT)) { //LOOP TO CYCLE THROUGH SQL RESULTS AND STORE Privlege information as vairable $priv. while ($row = $result->fetch_assoc()) { $priv === $row["su"]; } } // close SQL connection. mysqli_close($link); // Verify privleges and take action. Only a privlege of "1" is allowed to view this page. A privlege of "2" indicates special //accounts used in other scripts that have certain indermediate additional functions, but are not trusted administrators. if ($priv !== 1) { echo $_SESSION['name']; echo "you have privlege level of $priv"; echo "<br>"; echo 'Your account does not have the privleges necessary to view this page'; exit; } } verify(); ?>
I have a script that adds points together based upon the placing. This is the actual script: Code: [Select] <? $points = 0; if($place === '1st') {$points = $points + 50;} elseif($place === '2nd') {$points = $points + 45;} elseif($place === '3rd') {$points = $points + 40;} elseif($place === '4th') {$points = $points + 35;} elseif($place === '5th') {$points = $points + 30;} elseif($place === '6th') {$points = $points + 25;} elseif($place === '7th') {$points = $points + 20;} elseif($place === '8th') {$points = $points + 10;} elseif($place === '9th') {$points = $points + 10;} elseif($place === '10th') {$points = $points + 10;} elseif($place === 'CH') {$points = $points + 50;} elseif($place === 'RCH') {$points = $points + 40;} elseif($place === 'TT') {$points = $points + 30;} elseif($place === 'T5') {$points = $points + 30;} elseif($place === 'Champion') {$points = $points + 50;} elseif($place === 'Reserve Champion') {$points = $points + 40;} echo "Total HF Points: $points"; ?>What it *should* do (my friend's script works the same way and it works) it starts at points = 0, than if there is a first place, it adds 50, and so forth until it reaches the end. It is included into a file, in this area: Code: [Select] <div class="tabbertab"> <h2>Records</h2> <? $query92 = "SELECT * FROM THISTABLE WHERE VARIABLE='$id' OR VARIABLE = '$name' ORDER BY ABS(VARIABLE), VARIABLE"; $result92 = mysql_query($query92) or die (mysql_error()); echo "<table class='record'> <tr><th>Show</th> <th>Class</th> <th>Place</th></tr> "; while($row92 = mysql_fetch_array($result92)) { $class = $row92['class']; $place = $row92['place']; $entries = $row92['entries']; $race = $row92['show']; $purse = number_format($row92['purse'],2); echo "<tr><td>$race</td> <td>$class</td> <td>$place</td></tr>"; } ?> <tr><td colspan='3'><div align='right'><? include('includes/points.php'); ?></div></td></tr> </table> </div> This is the code that is relevant. When ended here, it echoes the last place that appears in the results (such as a 5th place echoing 30 points). When I move it to be included in the while loop, it shows Total Points: 50 Total Points: 25 Total Points: 10 (depending on the results displayed on that page). What am I doing wrong? Hello everyone, I can get Test 2 to successfully operate the if statement using a variable variable. But when I try the same method using a session variable (Test 1) the if statement is not executed. Please could you tell me why the if statement in Test 1 is not being executed? Code: [Select] <?php # TEST 1 $_SESSION[test_variable] = "abcd"; $session_variable_name = "_SESSION[test_variable]"; if ($$session_variable_name == "abcd") { echo "<br>line 373, abcd<br>"; } # TEST 2 $test_variable = "efgh"; $test_variable_name = "test_variable"; if ($$test_variable_name == "efgh") { echo "<br>line 379, efgh<br>"; } ?> Many thanks, Stu I have a form that creates rows of data input textboxes depending on a user input number of things. I have a naming convention for all these textboxes that basically just keeps incrementing a number suffix for each row. All this is working fine. My problem is I need to get the data inserted into this table of textboxes into an array. Here's my code where I attempt to to this (it does not work): Code: [Select] $temp = $_SESSION['Num_Part']; $count = 1; while ($count <= $temp){ $temp2[$count] = "'Participant_P".$count."'"; //echo $temp2[$count]."<br/>"; $temp3[$count]=$_POST[$temp2[$count]]; //here's the problem $temp4[$count] = "'Result_P".$count."'"; $temp5[$count]=$_POST[$temp4[$count]]; //here's the problem //echo $temp4[$count]."<br/>"; $count++; } The problem is that the $_POST does not work with the variable in the argument position - even though the argument is formatted with single quotes. Can a variable be used in a POST argument and if so what is the correct syntax? If not, is there some other simple solution to harvest the data into an array. I understand I can harvest by explicitly accessing each key in the post assoc array. But this could be dozens of rows of input fields. Thanks in advance for your help here. I couldn't find anything online re this topic. I have this javascript code which uses mouseover for a tooltip. It works fine until i add php. Anyone know how i could add the php variable? Here is the original code: <span class="users_browsing" onmouseover="tooltip.show('hi');" onmouseout="tooltip.hide();"> Ive tried this but it doesnt work: echo "<span class=\"users_browsing\" onmouseover=\"tooltip.show(\"{$content1}\");\" onmouseout=\"tooltip.hide();\"> Users Browsing Forum: {$browsers_num}</span>"; Quote i need to store a variable from database like if i have "copies" in one of my column in my database then i have to store a particular value for copies store it to $copies here i want that i can store value of copies into $copies $update_book="update book set copies=copies-1 where bookid='$bookid'"; $result=mysql_query($update_book,$linkID1); if($result) { print "<html><body background=\"header.jpg\"> <p>book successfully subtracted from database</p></body></html>"; } else { print "<html><body background=\"header.jpg\"> <p>problem occured</p></body></html>"; } } hi all, I have an language pack for example: languages/en.php: Code: [Select] $en['mail']['letter closing'] = "regards,\n your friend!"; and in my config: Code: [Select] $language = "en"; $include_language = @include("languages/".$language.".php"); if(!($include_language)) { $try_default_language = @include("languages/nl.php"); if(!($try_default_language)) { echo "kan de taalpakket niet vinden<br>"; echo "Could not find the language pack.<br>"; echo "example on error: ".$test." shows nothing"; exit; } } In my function I want to include the language pack for example i have $language = 'en' so I want to include $en['general']['letter closing'] I will do this: Code: [Select] global $language,${$language}['general']; But that gives an error unexpected '[' blah blah. How can i call the variable variable array in the valid php way? I have a function that get's a quick single item from a query: function gimme($sql) { global $mysqli; global $mytable; global $sid; $query = "SELECT ".$sql." FROM ".$mytable." WHERE sid = ".$sid; $result = $mysqli->query($query); $value = $result->fetch_array(MYSQLI_NUM); $$sql = is_array($value) ? $value[0] : ""; return $$sql; // this is what I've tried so far $result->close(); } It works great as: echo(gimme("name")); Then I realized that I could use that as a variable ('$name' in this case) elsewhere. However, I can't figure out how get that new, variable variable 'outside' of the function. As such, echo($name); isn't working outside the function. Is there a way to return a variable variable? In other words, is there a way to make a function that creates a variable variable that will available outside of the function?
Thanks
I just moved my code from Appserv to EasyPHP and it gave me this error, it was working fine on Appserv...what's with easyPHP ?? Probably something simple but I have searched high and low and can't figure this one out. I have a variable that is of the datetime format. I have another variable that is of the time format. I need to add them together. Example: $var1 = 2012-02-24 06:38:22 $var2 = 02:00:00 $var3 = $var1 + $var2 = 2012-02-24 08:38:22 Thanks for the help! Okay, I have been looking into this for the last 3 hours, and cant find shit. I have a page with a javascript game where the player moves around on a map. They have an X and Y coordinate, but I'm having a problem. The problem is I need to store that info in a database, and I'm having trouble learning AJAX. Heres a little list of things: Play loads the game. A php variable is taken from a database. The player goes to those X and Y coordinates. The player may move around. Once they move, the player is sent to a php page that updates the Database information. The page is refreshed to the game again, where they may then move around some more. The problem is, when the page is changed the values are lost. For some reasons cookies aren't working at all (like just not working for anything, even a completely different file.) I heard something about using $_GET or $_POST, but I dont see how... Well this is the code I currently have, it is derived from ajax organic tabs codes. Code: [Select] <? if($filename == "profile"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#profile").organicTabs(); }); </script> <? } ?> It may look fine this way, but rather inflexible if I wish to implement more tabs in future instead of just for the profile page. Assuming I have profile.php, members.php and stats.php and all of the three script files use tab system, the codes will become: Code: [Select] <? if($filename == "profile"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#profile").organicTabs(); }); </script> <? } elseif($filename == "members"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#members").organicTabs(); }); </script> <? } elseif($filename == "stats"){ ?> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script> <script src="js/tabs.js"></script> <script> $(function() { $("#stats").organicTabs(); }); </script> <? } ?> Which can get even more tedious as more and more pages needs to adopt tabs system. I wonder if there is a way to simplify the code by passing the php variable $filename directly into javascript tabs definition in this line, where the part $("#profile") changes as filename changes? Please help... Code: [Select] $("#profile").organicTabs(); hi is it possible to post a variable like this either with javascript or php query.php Code: [Select] <?php include("../../includes/config.php"); mysql_connect($db_address, $db_username, $db_password);mysql_select_db($db_name) or die(mysql_error()); $fid = $_GET['fid']; $fid = mysql_real_escape_string($fid); $query10 = "SELECT * FROM films WHERE (film_id = '$fid')"; $qry_result10 = mysql_query($query10) or die(mysql_error()); while($row10 = mysql_fetch_array($qry_result10)) { $urlfid="http://www.onfilm.biz/streaming/home/".$row10['client']."/".$row10['client_office']."/".$row10['filename'].".mp4";?> <a class="hofwhite14" href="#" onclick="loadNplay('<?php echo $urlfid; ?>')"><u></u>Property Film</u></a> <?php }mysql_close(); //EDIT 290510 ?> <script type="text/javascript"> var video= $urlfid; $.post('indextest.php', {urlfid:video}); </script> Hi; I'm a beginner and I'm getting a big headache figuring how to convert php variables into Javascript variables. I want to use the content of $theName as the value for an input tag. $_SESSION['$Sess_Name'] is loaded with the right data, I verified it. But my function InitField returns always the word NULL. Why is that? Where is the problem? Thank Here is my code: function InitField() { <?php can i add php variables into a js file? i have tried this: document.location = <?php echo $_SESSION['fwd_page']; ?>; but it doesnt work. Hi everybody! I am new to php, I'm trying to read a php variable using javascript function my code is as follow: <?php $php_var = 100; ?> <SCRIPT type="text/javascript"> var js_var = "<?php =$php_var; ?>"; function display_var() { alert(js_var); } </SCRIPT> <body onload="display_var()"> .......... .......... </body> the problem is that the line alert(js_var); gives me "<?php =$php_var; ?>" instead of 100. I don't know what I'm missing. any help would be greatly appreciated. Thanks in advance. I'm making a site where I use a javascript popup, and I want to pass in a php variable to the popup, but I'm not sure what to do. Normally, I would use $_GET but the link points to the javascript code. Basically, I want the popup to display which user was clicked, player 1 or player 2, but I don't know where to start. Sorry if this should be asked in a javascript forum, I wasn't sure who I should ask. If I've asked in the wrong place, let me know. Test.php Code: [Select] <html> <head> <script type="text/javascript" src="script/popup-window.js"></script> </head> <body> <?php $player1 = "abc"; $player2 = "def"; $javacode = "javascript:popup_show('popup', 'popup_drag', 'popup_exit', 'screen-top-left', 1200, 20)"; echo 'Player 1: <a href="'.$javacode.'">'.$player1.'</a><br />'; echo 'Player 2: <a href="'.$javacode.'">'.$player2.'</a><br />'; ?> <div class="sample_popup" id="popup" style="display: none;"> <?php echo "Player selected: "; ?> </div> </body> </html> script/popup-window.js Code: [Select] // Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved. // Technical support: http://www.php-development.ru/ // // YOU MAY NOT // (1) Remove or modify this copyright notice. // (2) Re-distribute this code or any part of it. // Instead, you may link to the homepage of this code: // http://www.php-development.ru/javascripts/popup-window.php // // YOU MAY // (1) Use this code on your website. // (2) Use this code as part of another product. // // NO WARRANTY // This code is provided "as is" without warranty of any kind. // You expressly acknowledge and agree that use of this code is at your own risk. // USAGE // // function popup_show(id, drag_id, exit_id, position, x, y, position_id) // // id - id of a popup window; // drag_id - id of an element within popup window intended for dragging it // exit_id - id of an element within popup window intended for hiding it // position - positioning type: // "element", "element-right", "element-bottom", "mouse", // "screen-top-left", "screen-center", "screen-bottom-right" // x, y - offset // position_id - id of an element relative to which popup window will be positioned // ***** Variables ************************************************************* var popup_dragging = false; var popup_target; var popup_mouseX; var popup_mouseY; var popup_mouseposX; var popup_mouseposY; var popup_oldfunction; // ***** popup_mousedown ******************************************************* function popup_mousedown(e) { var ie = navigator.appName == "Microsoft Internet Explorer"; popup_mouseposX = ie ? window.event.clientX : e.clientX; popup_mouseposY = ie ? window.event.clientY : e.clientY; } // ***** popup_mousedown_window ************************************************ function popup_mousedown_window(e) { var ie = navigator.appName == "Microsoft Internet Explorer"; if ( ie && window.event.button != 1) return; if (!ie && e.button != 0) return; popup_dragging = true; popup_target = this['target']; popup_mouseX = ie ? window.event.clientX : e.clientX; popup_mouseY = ie ? window.event.clientY : e.clientY; if (ie) popup_oldfunction = document.onselectstart; else popup_oldfunction = document.onmousedown; if (ie) document.onselectstart = new Function("return false;"); else document.onmousedown = new Function("return false;"); } // ***** popup_mousemove ******************************************************* function popup_mousemove(e) { var ie = navigator.appName == "Microsoft Internet Explorer"; var element = document.getElementById(popup_target); var mouseX = ie ? window.event.clientX : e.clientX; var mouseY = ie ? window.event.clientY : e.clientY; if (!popup_dragging) return; element.style.left = (element.offsetLeft+mouseX-popup_mouseX)+'px'; element.style.top = (element.offsetTop +mouseY-popup_mouseY)+'px'; popup_mouseX = ie ? window.event.clientX : e.clientX; popup_mouseY = ie ? window.event.clientY : e.clientY; } // ***** popup_mouseup ********************************************************* function popup_mouseup(e) { var ie = navigator.appName == "Microsoft Internet Explorer"; var element = document.getElementById(popup_target); if (!popup_dragging) return; popup_dragging = false; if (ie) document.onselectstart = popup_oldfunction; else document.onmousedown = popup_oldfunction; } // ***** popup_exit ************************************************************ function popup_exit(e) { var ie = navigator.appName == "Microsoft Internet Explorer"; var element = document.getElementById(popup_target); popup_mouseup(e); element.style.display = 'none'; } // ***** popup_show ************************************************************ function popup_show(id, drag_id, exit_id, position, x, y, position_id) { var element = document.getElementById(id); var drag_element = document.getElementById(drag_id); var exit_element = document.getElementById(exit_id); var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth; var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight; element.style.position = "absolute"; element.style.display = "block"; if (position == "element" || position == "element-right" || position == "element-bottom") { var position_element = document.getElementById(position_id); for (var p = position_element; p; p = p.offsetParent) if (p.style.position != 'absolute') { x += p.offsetLeft; y += p.offsetTop; } if (position == "element-right" ) x += position_element.clientWidth; if (position == "element-bottom") y += position_element.clientHeight; element.style.left = x+'px'; element.style.top = y+'px'; } if (position == "mouse") { element.style.left = (document.documentElement.scrollLeft+popup_mouseposX+x)+'px'; element.style.top = (document.documentElement.scrollTop +popup_mouseposY+y)+'px'; } if (position == "screen-top-left") { element.style.left = (document.documentElement.scrollLeft+x)+'px'; element.style.top = (document.documentElement.scrollTop +y)+'px'; } if (position == "screen-center") { element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )/2+x)+'px'; element.style.top = (document.documentElement.scrollTop +(height-element.clientHeight)/2+y)+'px'; } if (position == "screen-bottom-right") { element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth ) +x)+'px'; element.style.top = (document.documentElement.scrollTop +(height-element.clientHeight) +y)+'px'; } drag_element['target'] = id; drag_element.onmousedown = popup_mousedown_window; exit_element.onclick = popup_exit; } // ***** Attach Events ********************************************************* if (navigator.appName == "Microsoft Internet Explorer") document.attachEvent ('onmousedown', popup_mousedown); else document.addEventListener('mousedown', popup_mousedown, false); if (navigator.appName == "Microsoft Internet Explorer") document.attachEvent ('onmousemove', popup_mousemove); else document.addEventListener('mousemove', popup_mousemove, false); if (navigator.appName == "Microsoft Internet Explorer") document.attachEvent ('onmouseup', popup_mouseup); else document.addEventListener('mouseup', popup_mouseup, false); |