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>';
}
?>
Related
I have an array of texts which I have kept in an array. The array is linked to a button and when that button is pressed, I'd like to open all the links in different tabs.
e.g
if(isset($_POST["open links"]))
{
foreach($array as $item)
{
<a href="$item" target="_blank" ></a>
}
}
The links are saved on a text file from a previous form and each item in the array is just the URL. How would I go about doing this?
How about trying to echo them
foreach($array as $item)
{
echo('<a href="' . $item . '" target="_blank" ></a>');
}
It prints the link tags with values of $item as target.
How about this
<?php foreach($array as $item)
{
?>
<script>
window.onload = function(){
window.open("<?=$item?>", "_blank"); // will open new tab on window.onload
}
</script>
<?php } ?>
To open multiple links at the same time you will need some (basic) javascript.
Try something like this:
<?php
$array = array( 'http://www.stackoverflow.com', 'http://www.google.com');
?>
<button id="my-button">Click me</button>
<script type="text/javascript">
var links = [
<?php
foreach($array as $i => $link)
echo '"' . $link . '"' . ($i < (sizeof($array) -1)? ',' : '');
?>
];
document.getElementById("my-button").onclick = function(){
links.forEach(function(link) {
window.open(link, '_blank');
});
}
</script>
Note that Chrome popup blocker doesn't let you programmatically open multiple new tabs at once, though. (Window.open isn't working for multiple links in Google Chrome)
consider your link file :-
links.txt
> http://google.com http://stackoverflow.com http://facebook.com
All links with sperated by space
then php code :-
<button id="my-button">Click me</button>
<script type="text/javascript">
document.getElementById("my-button").onclick = function(){
<?php foreach($links as $link) { echo"window.open(" . $link . ", '_blank');"; } ?>
}
</script>
Some code stolen from other answers :p , but that's a good practice ! Thanks :)
Kindly pls have a look at the code below ! Its working perfectly except the php code is not giving any output. When i insert the output of the php , the script work perfectly.
<script type="text/javascript">
function LoadVideoBar() {
var videoBar;
var barContainer = document.getElementById("videoBar");
var options = {
largeResultSet : false,
horizontal : true,
autoExecuteList : {
cycleTime : GSvideoBar.CYCLE_TIME_SHORT,
cycleMode : GSvideoBar.CYCLE_MODE_LINEAR,
executeList : [ "<?php $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name; echo "-"; echo wp_title(); ?> "]
}
}
new GSvideoBar(
document.getElementById("videoBar"),
document.getElementById("videoPlayer"),
options
);
}
GSearch.setOnLoadCallback(LoadVideoBar);
When i replace the php code
<?php $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name; echo "-"; echo wp_title(); ?>
with some text like : categoryname-title name
The script works perfectly.
Can somebody help me out with this small issue ...
Many Thanks in Advance!
If you ever need to pass data between the two. Put the PHP value into a hidden field. then read it like you did with bar container
Save the value from PHP:
<?php $cat = get_the_category(); $cat = $cat[0]; ?>
<input id="executeListValue" type="hidden"
value="<?php echo $cat->cat_name."-".wp_title();?>" >
read it in js:
var executeListValue = document.getElementById("executeListValue").value;
//...
autoExecuteList : {
cycleTime : GSvideoBar.CYCLE_TIME_SHORT,
cycleMode : GSvideoBar.CYCLE_MODE_LINEAR,
executeList : executeListValue
}
if the php code is replaced and the script works fine means php is having some error while executing. can you please try to see the source code of the html page. this may be happening because of some php error happening and those error message string could be making some syntax error to javascript.
try to put the php code outside the javascript tag and see the output generated from it.
<?php
$cat = get_the_category();
$cat = $cat[0];
$output= $cat->cat_name."-".wp_title();
?>
and then try to print the output in the javascript.
executeList : [ "<?php print $output; ?>"]
}
by this way you can see if any php error is encountered.
Replace "-" with '-' in Javascript code in line:
[ "<?php $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name; echo "-"; echo wp_title(); ?> "]
I am trying to pass $post; a variable created in a mysql query, to javascript function showDiv.
Currently this doesnt work.
$post = $row['id'];
?>
<script type="text/javascript">
function showDiv() {
var note = "<?php echo $post ?>";
document.getElementById("<?php echo $post; ?>").style.display = "inline";
}
</script>
<?php
$addnote = '<input type="button" value="addnote" onclick="showDiv()"><div id="'.$postid.'" style="display:none;" class="'.$postid.'"> WELCOME</div>';
But if I change $post to have a html value e.g
$post = '11';
then this code works.
I am novice in javascript so please be gentle,
Any help is greatly appreciated.
Thank you.
If you are in a loop, I think JS doesn't like redeclare your function "showDiv()". Try this :
$post = $row['id'];
$addnote = '<input type="button" value="addnote" onclick="showDiv('.$post.')"><div id="'.$post.'" style="display:none;" class="'.$post.'"> WELCOME</div>';
And the javascript NOT in the loop :
<script type="text/javascript">
function showDiv(note) {
document.getElementById(note).style.display = "inline";
}
</script>
check your $row['id'] if it's returning something.
<?php echo $row['id']; ?>
or check your source code.
your code might look something like
<script type="text/javascript">
function showDiv() {
var note = "";
document.getElementById("").style.display = "inline";
}
</script>
Assuming that: $post = $row['id'] has a value. You want var note to have a value of a string. JS wraps strings in single or double quotes. so wrap <?php echo $post ?> in a string like this:
var note = "'"+<?php echo $post ?>"'";
This will prepend and append the quotes around the $post value so that JS can recognize it as a string.
I am having an issue with attempting to show different confirm text from an array when using a hyperlink. The text always ends up being from the last confirmation text in the array. I have seen 2 examples on this forum using a function() in a function but I was not able to get this working from viewing the examples.
Here is my code:
echo '
<script type="text/javascript">
function getDetails(message)
{
if (confirm(message))
return true;
else
{
var links = document.getElementsByTagName("a");
for(i=0;i<links.length;i++)
links[i].href = item_NoLink;
}
}
</script>';
foreach ($items as $item)
{
$link = 'http://test_url/mytest.php;report='. $item['id'];
echo '
<script type="text/javascript">
var item_detail = ', json_encode($item['reported_spam']['detail']),'
var item_NoLink = ', json_encode('http://test_url/mytest.php;'),'
</script>
<a id="mylink[]" onclick="getDetails(item_detail);" href="'.$link.'" style="text-decoration:none;">
<img id="myImage" alt="" src="http://test_url/images/reported.gif" title="'.$item['reported_spam']['title'].'" style="position:relative;border=0px;vertical-align:middle;right:5px;" />
</a>';
}
Thanks.
Edit: I figured it out.
#Grant Zhu: Arrays are not written like that in php and one can progress to the next key just using the empty square brackets. You were correct as I did make an err for the image id array and the js variables. Also for php when using single quotes inside echo with single quotes one must use the backslash (unless using php again).
I got it working as such:
echo '
<script type="text/javascript">
var item_NoLink = ', json_encode('http://test_url/mytest.php;'),'
function getDetails(message)
{
if (confirm(message))
return true;
else
{
var links = document.getElementsByTagName("a");
for(i=0;i<links.length;i++)
links[i].href = item_NoLink;
}
}
</script>';
foreach ($items as $item)
{
$link = 'http://test_url/mytest.php?report='. $item['id'];
echo '
<a id="mylink[]" onclick="getDetails(\'',$item['reported_spam']['detail'],'\');" href="'.$link.'" style="text-decoration:none;">
<img id="myImage[]" alt="" src="http://test_url/images/reported.gif" title="'.$item['reported_spam']['title'].'" style="position:relative;border=0px;vertical-align:middle;right:5px;" />
</a>';
}
Thank you.
$link = 'http://test_url/mytest.php;report='. $item['id'];
this code is weird , I think your code might be
$link = 'http://test_url/mytest.php?report='. $item['id'];
You should check the javascript generated and you will find there're multiple declarations of item_detail and item_NoLink. That means you assign the values to the same variables again and again. Of course, the last assignment takes effect in the end.
You can put the detail text directly in the getDetails function. Make sure the text is quoted by '. And you'd better make the id of <a> and <img> unique because that's what id means. I'm not familiar with PHP, check the syntax below if it's correct.
foreach ($items as $item)
{
$link = 'http://test_url/mytest.php;report='. $item['id'];
echo '
<a id="mylink$item['id']" onclick="getDetails(', json_encode($item['reported_spam']['detail']),');" href="'.$link.'" style="text-decoration:none;">
<img id="myImage$item['id']" alt="" src="http://test_url/images/reported.gif" title="'.$item['reported_spam']['title'].'" style="position:relative;border=0px;vertical-align:middle;right:5px;" />
</a>';
}
I have a PHP and jQuery script that creates search result suggestions from a text box. However, when you type something in the text box, delete it and try making a different query, no suggestions are displayed. Why could this be?
Here is a copy of my webpage code:
<script type="text/JavaScript">
function lookup(inputString){
if (inputString.length==0){
$('#suggestions').hide();
} else{
$.post("suggestions.php",{
queryString: "" + inputString + ""},
function(data){
$('#suggestions').html(data);
});
}
}
</script>
<form>
<input type="text" size="30" onkeyup="lookup(this.value);">
<div id="suggestions"></div>
</form>
Here is a copy of my PHP code:
<p id="searchresults"><?php
$db=new mysqli('localhost','username','password','database');
if(isset($_POST['queryString'])){
$queryString=$db->real_escape_string($_POST['queryString']);
if(strlen($queryString)>0){
$query = $db->query("SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'");
if($query){
while ($result = $query ->fetch_object()){
echo '<a href="'.$result->name.'">';
$name=$result->name;
echo ''.$name.'';
}
}
}
}
?></p>
Thanks in advance, Callum
You hide but don't show again.
change your callback function to:
function(data){
$('#suggestions').html(data).show();
}
or a fadeIn() for added cuteness ;)
Add $('#suggestions').show() inside of your else statement.