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
Related
I have an echo from a PHP script which is this:
echo "<select class='form-control' id='backgroundcolor' onchange='uploadFile()'>";
foreach($backgroundcolors as $cc => $name) {
echo '<option value="' . $name . '">' . $cc . '</option>';
}
echo "</select>";
It returns the following:
<select class='form-control' id='backgroundcolor' onchange='uploadFile()'><option value="#CD5C5C">Indian Red</option><option value="#000000">Black</option></select>
I then use the following code to add it to a div:
_("backgroundcolor").innerText = event.target.responseText;
Where I have defined the following function _:
function _(el) {
return document.getElementById(el);
}
BUt instead of giving me a select option it just shows the html in plain text on the site. If I copy the code and paste it to my site manually it works.
Use innerHTML instead of innerText:
_("backgroundcolor").innerHTML = event.target.responseText;
innerText adds the content as plain text, while innerHTML adds the content as it is without encoding html characters.
You can use _("backgroundcolor").innerHTML = event.target.responseText; But if you are using jquery, you can also use $('#backgroundcolor').html(event.target.responseText); without those function above (function _(el)).
I am trying to pass a variable from jquery to php
My jquery code:
<script>
$(window).load(function(){
$.get("as.php",url="http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery", function(data,status){
$('#lax').html(data);
});
});
</script>
<div id="lax" >
</div>
And my "as.php" file is as follows:
<?php
include('phpQuery.php');
$file = mysqli_real_escape_string($dbc, strip_tags(trim($_GET['url'])));
phpQuery::newDocumentFileHTML($file);
$titleElement = pq('title');
$title = $titleElement->html();
echo '<a href="" >' . htmlentities( $title) . '</a><br/>';
foreach(pq('meta') as $li)
if ((pq($li)->attr('name')=='description')||(pq($li)->attr('name')=='Description')){
echo '<p>'.pq($li)->attr('content').'</p>';
}
?>
I am tryin to pass 'url' variable from jquery code to my "as.php" file , but not able to do so. Where must be I going wrong?
You need to create an object
url="http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery"
Should be:
{url :"http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery"}
jQuery docs
I don't see you opening a database connection, so with the code you posted $dbc will be NULL.
That causes mysqli_real_escape_string to return NULL as well.
As you are not doing any database operations, you should get rid of mysqli_real_escape_string completely.
in you jquery:
<script>
$(window).load(function(){
$.get("as.php",
{url:"http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery"}, function(data){
$('#lax').html(data);
});
});
</script>
in your php try using $_REQUEST
<?php
include('phpQuery.php');
$file = mysqli_real_escape_string($dbc, strip_tags(trim($_REQUEST['url'])));
phpQuery::newDocumentFileHTML($file);
$titleElement = pq('title');
$title = $titleElement->html();
echo '<a href="" >' . htmlentities( $title) . '</a><br/>';
foreach(pq('meta') as $li)
if ((pq($li)->attr('name')=='description')||(pq($li)->attr('name')=='Description')){
echo '<p>'.pq($li)->attr('content').'</p>';
}
?>
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);
I am pulling out record from the database and inserting them inside a dropdown like this:
echo "<select>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option>" . $drow['id'] . "</option>";
}
echo "</select>";
It works but I need to be able to click on an option on the dropdown and make it link just like:
Record1Here
Record2Here
Record3Here
UPDATE: Latest code:
<script>
function doSomething() {
var currentval = this.options[this.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
</script>
...
echo "<select onchange='doSomething();'>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option value=\"view.php\">" . $drow['id'] . "</option>";
}
echo "</select>";
You can't place anchors on an option within a select list.
What you can do is use JavaScript and then do something on the change event of the select list :
echo "<select onchange='doSomething(this);>';
then in JavaScript do something based on the selected value :
function doSomething(elem) {
var currentval = elem.options[elem.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
Example here
you could update your PHP code to include a value in each option :
echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";
I have a php loop that is echoing out geolocation values. How can I get it to write those values to a javascript array, so I can then use them to plot points on an HTML5 canvas?
The php loop is as follows
<ul id = "geo-list">
<?php foreach($data as $phrase) { ?>
<li><?php
if ($phrase->geo != false) {
echo " | From (";
echo $phrase->geo->coordinates[0];
echo ",";
echo $phrase->geo->coordinates[1];
echo ")";
} else {
echo " | No Location Data";
}
?>
</li>
<?php } ?>
</ul>
Did you try
var myJavascriptData = <?= json_encode($php_data) ?>;
You might want to take advantage of the JSON library for PHP.
The cleanest way to pass data to a browser's javascript program is to put it into a "hidden" html table.
The html should look something like
echo "\n<TABLE style='display: none;' id='DATTAB' >" ;
get_rows();
while ($cf = next_row()) {
echo "\n <TR>";
echo "\n<TD>" . $cf['KEY'] . "</TD>";
echo "\n<TD>" . $cf['COL1'] . "</TD>";
echo "\n<TD>" . $cf['COL2'] . "</TD>";
echo " </TR>";
}
echo "\n</TABLE>";
This table is then easily accessable from your javascript:-
var dtab = document.getElementById("DATATAB");
var rows = dtab.getElementsByTagName("tr");
for (var r = 0; r < rows.length ; r++) {
row = rows[r];
item_key = row.cells[0].innerHTML;
item_col1 = row.cells[1].innerHTML;
item_col2 = row.cells[2].innerHTML;
// do your thing here ......
}
Alternatively you could look at using the AJAX libraries like prototype or dojo
which have the all javascript components for accessing data from a "REST" type service.
You then need to write a separate service which gets the XML or JSON required for your page.
My suggestion is to dump a script block to the output and set them in a variable there.
The array definition will have to actually be in the javascript code that gets output to the page.
e.g., you'll need an output of something like:
<script type="text/javascript">
var coords = new Array(2);
coords[0] = new Array(2);
coords[0][0] = 123.45;
coords[0][1] = 987.65;
coords[1] = new Array(2);
coords[1][0] = 234.56;
coords[1][1] = 876.54;
</script>
There are more efficient ways to create this array statically, but this is just an example.
A more efficient way (in terms of code) would be to build up a string that defined the literal array, then dump out a javascript definition. The output would be something like:
<script type="text/javascript">
var coords = [[123.45,987.65],[234.56,876.54]];
</script>
so in your loop within php, you'd build up a string which would ultimately contain var coords = [[123.45,987.65],[234.56,876.54]]. Outside your loop, you wrap it in the script blocks and output it to the page.