I must be tired or something because i am unable to get this line of code to work:
var all = color.val('all');
$('#cssColor" + <?php echo $page ?> + "', parent.document).attr("background-color", all.hex);
I even have a textbox with the page value as well and i try:
var all = color.val('all');
$('#cssColor" + $('#txtPageValue').val() + "', parent.document).attr("background-color", all.hex);
I can not seem to send the page value!
Try to change this one:
$('#cssColor" + <?php echo $page ?> + "',
to:
$('#cssColor<?php echo $page ?>',
And also, for the second one:
$('#cssColor" + $('#txtPageValue').val() + "',
to:
$('#cssColor' + $('#txtPageValue').val(),
I am not sure how you are assigning the all variable, but assuming it is getting assigned correctly, you could rewrite your code something like this to get the value to appear in the right place in your javascript:
<?php echo "<script type='text/javascript'>
//code somewhere in here should define the color object
var all = color.val('all')
$('#cssColor" . $page . "', parent.document).attr('background-color', all.hex);
</script>"; ?>
This writes the javascript to the document, without breaking the echo function in the middle.
Or, you could do this:
var all = color.val('all');
$('#cssColor'+<?php echo $page; ?>, parent.document).attr('background-color', all.hex);
Related
I would like to link a variable to a link.
The variable looks something like this which I grab from json.
The json variable for $movieSeat is : A1,A2,B3,V4
Below is the code that I've tried out.
The issue that I am currently having is, the link doesn't seem to be working. It seems like something to do with . and +.
I'm also not too sure when to use . and + for the linking.
<?php
echo "<script> function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=" . $movieSeat. "'; } </script>";
for ($i = 0; $i < $jsonCounter; $i++) {
$movieSeat = $jsonValue[$i]->seatID;
echo "<button onclick = movieBTN(".$movieSeat.")> Movie Seat </button>";
}
?>
First of all, if you're echoing the function from PHP, make sure you use JS variables and not PHP's:
echo "<script> function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=' + x; } </script>";
So it will output:
<script>
function movieBTN(x) { location.href = 'http://movie.com/movieOne?seatNum=' + x; }
</script>
Next, you need to wrap the variable passed to that function with ':
echo "<button onclick = movieBTN('".$movieSeat."')> Movie Seat </button>";
So it will output as:
<button onclick = movieBTN('A1')> Movie Seat </button>
I'm building a site that will (eventually) be the front-end for a game. I want to be able to dynamically build a list of "powers" that the user is able to purchase. In order to build these lists, I'm using a PHP-based SQL query, then passing it to Javascript for dynamic choices (some powers have prerequisite powers).
I know there's a simpler way to do what I'm doing, but I'm not super concerned with that right now (I will be later, but I'm trying to get this functional and then clean) (again, I know this is non-optimal).
I'm using eval to parse which divs to show, and I want to know how not to.
I'm having issues getting my div names built right in the first place.
Here's my code:
Javascript (separate file)
function upgradeList(passed)
{
var PowerArray = [];
var DetailPowerID = [];
var EscapedPowerID = [];
PowerArray.push([]);
PowerArray = eval(passed);
var OutputThing="";
for (i=0;i<PowerArray.length;i++)
{
DetailPowerID[i] = 'detail' + PowerArray[i][0];
EscapedPowerID[i] = "'" + DetailPowerID[i] + "'";
}
for (i=0;i<PowerArray.length;i++)
{
OutputThing = OutputThing + "<br><a href='#' onClick='showUpgradeDetails(" + DetailPowerID[i] + ")'>" + PowerArray[i][2] + "</a><div class='hidden' id='" +
DetailPowerID[i] + "'>" + PowerArray[i][3] + "</div>"; }
document.getElementById("secondUpgrade").innerHTML=OutputThing;
document.getElementById("secondUpgrade").style.display='block';
}
}
PHP writing HTML and JS:
{$AbleToUpgrade and $UpgradeList are both 2d arrays built from SQL queries)
echo "<script name='UpgradeList'>";
settype($UpgradesListSize[$i],"int");
for ($i=0;$i<count($AbleToUpgrade);$i++)
{
echo "var UpgradeList" . $AbleToUpgrade[$i][0] . " = new Array();";
for ($j=0;$j<=$UpgradesListSize[$i];$j++)
{
echo "UpgradeList" . $AbleToUpgrade[$i][0] . ".push(Array('"
. $UpgradeList[$i][$j][0] . "', '"
. $UpgradeList[$i][$j][1] . "', '"
. $UpgradeList[$i][$j][2] . "', '"
. $UpgradeList[$i][$j][3] . "', '"
. $UpgradeList[$i][$j][4] . "'));";
}
}
echo "</script>";
... and, later...
echo "<div id='SpendUpgrade'>
Select power to upgrade:
<ul>";
for ($i=0;$i<count($AbleToUpgrade);$i++)
{
echo "<li><a href='#' name='UpgradeList" . $AbleToUpgrade[$i][0] . "' onClick='upgradeList(this.name)'>" . $AbleToUpgrade[$i][1] . " - " . $AbleToUpgrade[$i][2] . "</a></li>";
}
echo "</select>
<div id='secondUpgrade' class='hidden'>
</div>
<div id='thirdUpgrade' class='hidden'>
</div>
</div>";
When I load the page, I wind up with generated text like this:
Real Armor
and the corresponding div:
<div class="hidden" id="detail21" style="display: none;">Your armor only works in the Waking</div>
In order to get the div to show (display:block;), I need to call the function like so:
showUpgradeDetails("detail21")
but I can't make JS / PHP write the quotes correctly. Help (with any or all of this?) please!
I found a resolution, and it wasn't JSON.parse().
I changed PowerArray = eval(passed); into PowerArray = window[passed];.
Because passed contains the name of a variable, and is not the variable itself, I couldn't work directly with it. However, because it was a string that held exclusively the name of a globally-defined variable, I could pass it to the window[] construct and have it work.
The following works well:
var options = '<select><option value ="Unknown">Unknown</option><option value ="Yes">Yes</option><option value ="No">No</option><option value ="Both">Both</option></select>';
alert(options)
However, if that string is produced via a PHP foreach, the variable options is not recognized, hence I cannot use with JS. For example:
<?php foreach($pickListFields as $field_id => $options): ?>
<?php
$options_array = explode("\n", $options);
$options_select = '<select>';
foreach($options_array as $k => $option) {
$options_select .= '<option value ="' . $option . '">' . $option . '</option>';
}
$options_select .= '</select>';
?>
var options = '<?= $options_select ?>';
<?php endforeach; ?>
The above variable options produced, does not work, even though when I see the source code with Firefox I can see that var options is:
var options = '<select><option value ="Unknown">Unknown</option><option value ="Yes">Yes</option><option value ="No">No</option><option value ="Both">Both</option></select>';
Why then it cannot be used, if it is the same as the first example? I cannot alert that, or assign it to a field, but I can with the first example.
You are using php tags inside php instead of javascript tags:
var options = '<?= $options_select ?>';
should be:
echo '<script language="javascript" type="text/javascript">var options="' . $options_select . '";</script>';
try maybe html encode all output options, there can be some special chars hidden somewhere
I've got the following code, and the stuff before the javascript echos and the stuff after the javascript includes, but the javascript won't echo :/
$currentPage = $_POST["current_page"];
$nextPage = 1 + $currentPage;
$count = $_POST["cum_count"];
$total = $_POST["cum_total"];
$progress = $_POST["cum_progress"];
echo $currentPage . $nextPage;
# number of questions less 1
$numQs[2]=6;
$numQs[3]=3;
$numQs[4]=5;
$numQs[5]=34;
$numQs[6]=17;
$numQs[7]=43;
$falses = array('false');
for ($i=0; $i < $numQs[$nextPage]; $i++) {
array_push($falses,', false');
}
# the js is how the survey keeps track of where it is
echo "<script type='text/javascript'>\n
var c_name = 'whbssurvey';\n
var c_value = '$nextPage';\n
document.cookie=c_name + '=' + c_value;\n
// set survey info\n
var count = $count;\n
var total = $total;\n
var progress = $progress;\n
var qArray = [$falses];\n
</script>";
include("$nextPage.php");
P.S. In case anyone is thinking cum_count is something dirty, it's short for cumulative.
Are you sure it's not echoing? It's being done BEFORE you do your include. If that include is a complete html page, the JS would be echoed BEFORE the opening <html> tag (which makes for an invalid page).
As well, for dumping out multiline text like that, you should either drop out of PHP mode so it's just plaintext that'll get echoed out automatically, or use a HEREDOC. Since you're inserting a couple PHP vars into that output, the HEREDOC would probably be preferable.
Try echoing without the <script> and </script> tags. It's possible that it is being printed but your browser isn't rendering it for some reason. If you get all the code spewed on the page, it worked.
try:
echo "<script type='text/javascript'>\n" .
"var c_name = 'whbssurvey';\n" .
"var c_value = '$nextPage';\n" .
"document.cookie=c_name + '=' + c_value;\n" .
"// set survey info\n" .
"var count = $count;\n" .
"var total = $total;\n" .
"var progress = $progress;\n" .
"var qArray = [$falses];\n" .
"</script>";
Well I've managed to put the following together from various examples, however I don't believe the javascript function is even being called, below is all my code:
Javascript:
<script type="text/javascript" src="coding/jquery.js"></script>
<script type="text/javascript">
function showHint(quantity, price){
document.getElementById("txtcost").innerHTML="being called values" + quantity + price;
$.getJSON('http://website.com/ecommerce/coding/validation/calprice.php', {q: quantity, p: price}, function(data){
$('#txtcost').html('Total: ' + data.total);
$('#txtvat').html('VAT: ' + data.vat);
});
}
</script>
Display
<span id="txtcost" style="color:#060"></span>
<span id="txtvat" style="color:#060"></span>
PHP
<?php
echo "being called";
function data() {
$q = $_GET["q"];
$p =&$_GET["p"];
$total = $p * $q;
if($q >= 10)
{
$total = ($total / 10) * 9;
//echo '<strong>£ ' . $total . '</strong><span style="font-size:10px; font-weight:bold;"> Discount applied.</span>';
echo json_encode(array('total' => $total, 'vat' => '10.00'));
}
else{ echo '<strong>£ ' . $total . '<strong>'; }}
?>
Hope that helps
Note: the code isn't perfect, I want to get the initial functionality working.
EDIT:
SO far I've managed to establish:
The function is being called
The correct values are being passed into the function
For one thing, you need to use JavaScript's dot notation to reference object properties, and not PHP's arrow operator:
data.total
instead of
data->total
i think a problem with your quotes on line 1 you seem to be breaking into php, maybe you need to use something more like:
<input name="quantity" type="text" id="quantity" onkeyup="showHint(this.value, ' . $events[0]['price'] . ' )" size="6" />
but hard to say without full code