Pass Jquery + Php search back to javascript? - php

I have a javascript function as follows::
//THIS IS javascript.js
function dosomething(data){
//splits and do something else
}
$(document).ready(function () {
dosomething();
}
Below is a php file from search(database search with jquery and ajax)
//THIS IS mysearch.php
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$url = $row['url'];
$text = $row['text'];
$argument = $url"."$separator"."$text";
);
How do I pass $argument to javascript function? I have tried something like this.
echo '<p>'.$text.'</p>';
What would be good way to approach this?
Any help would be appreciated! Thanks in advance!!

This should do the trick:
echo '<p>'.$text.'</p>';
// Edit:
If you run into problems with special characters like öäü… you can use json_encode()

A lot of people like using json and ajax.
http://api.jquery.com/jQuery.getJSON/
http://php.net/manual/en/function.json-encode.php

Related

Read data from mysql and pass variable to jquery function, write in a textarea, not work

I am a beginner in jquery. I'm sorry for my english.
I read data from mysql:
$id=($row["ID"]);
$name=($row["Name"]);
$note=($row["Note"]); *// note set to textarea*
send function:
onclick= modP($id,$name,$note);
<script>
function modP(id,name,note){
$("#NoteP").val(note);
}
</script>
----- PROBLEM -------
NoteP is textarea.
if $note has one line the function work. all OK.
if $note has many lines the function modP is not called.
I tried with:
$note = str_replace("\n","<br />", $row["Note"]);
Not work!
Please help me. Thank you.
Try to encode your string to JSON in the PHP part then decode in JS part.
PHP :
$note= json_encode($row["Note"]);
JS :
$("#NoteP").val( JSON.parse(note) );
use nl2br on $note to break multines as below.
$id=$row["ID"];
$name=$row["Name"];
$note=$row["Note"];
$note=nl2br($note);
You can call onclick function on your html element as below.
<button onClick="modP('<?php echo $id ?>','<?php echo $name ?>','<?php echo $note ?>');">Click</button>
And the modP function will be as shown below.
<script type="text/javascript">
function modP(id,name,note) {
$("#NoteP").val(note);;
}
</script>
I hope this helps.
I found this suggestion
Link
Jquery function does not accept variables on multiple lines.
$note=str_replace("\n","<br />",$row["Note"]);
Now the function is called correctly.
($note) is one line.
How can I write textarea restoring original text, with many lines?
$note = json_encode($row["Note"], JSON_HEX_QUOT | JSON_HEX_TAG);
// note set to textarea - multiline
pass $data to the function modPaziente:
echo("<td><input name=\"imgmodifica\" id=\"imgmodifica\" type=\"image\" src=\"image/modifica2.png\" width=\"24\" height=\"24\" title=\"Elimina ".$row["ID_P"]."\" onclick=\"return modPaziente('$id','$nom','$ind','$comID','$citta','$prov','$tel','$cf','$ese','$cons','$nato','$sesso','".$note."','$eta')\"> </input></td>");
JS - /* alert(note) NOT work */
<script type="text/javascript">
function modPaziente(id,nom,ind,comID,citta,prov,tel,cf,ese,cons,nato,sesso,note,eta){
alert(note);
}
</script>
why modPaziente does not read note?
I solved that with htmlspecialchars and json_encode :
$note = htmlspecialchars(json_encode($row["Note"], JSON_HEX_QUOT | JSON_HEX_TAG));
...without quotes $note
onclick="return modPaziente($note);"
....
<script type="text/javascript">
function modPaziente(note){
alert(note);
}
Thank you all for the suggestions

Passing variable through function Javascript / PHP

i want to pass a variable through to a php on an onClick event, it doesn't seem to be passing it through to the page properly though.
Here's my html
<a href="data.php" onClick="video(We Have a Pope)" hidefocus="">
and the relevant javascript function
function video(result) {
$('#information').load('data.php?result=' + result);
document
}
My php page looks like this
<?php
$result = $_GET['result'];
echo $result;
?>
Nothing is being outputted though :S
onClick="video(We Have a Pope)" should be onClick="video('We Have a Pope')"
You need quotation marks when you pass string as parameter, like this:
<a href="data.php" onClick="video('We Have a Pope')" hidefocus="">
<a href="data.php" onClick="javascript:video('We Have a Pope');" hidefocus="">
onClick="video('We Have a Pope');"
or
onClick="video(\"We Have a Pope\");"

PHP array to echo javascript array that I can access?

I have a php array called $user.
At the minute I am getting the variables across to javascript by manually adding them like so.
var username = ' . $user['username'] . ';
And so on is there a way I can make php echo a javascript array that I can access?
You're looking for json_encode
PHP:
$json_user = json_encode($user);
JavaScript:
var user = JSON.parse('<?php echo $json_user; ?>');
That's untested code but the idea behind it is sound
Have you tried json_encode ?
<?php echo json_encode ( $array ); ?>
See the documentation here.

Jquery to parse HTML in a string

I tried searching the related posts, and having a hard time figuring out how to fix my query - I'm pretty close, any help is much appreciated (new to Jquery).
I program in PHP, and trying to pull either the HREF value from a tag, or the text. Either will work.
I basically have my HTML code in a string, might contain multiple tags, and would like to load the text of the tags into either a PHP array or variable (right now just trying to ALERT the results, I can dump it later).
My PHP Code:
<?php
$info = '<li><strong>I want this text</strong>';
echo '<script type="text/javascript">';
echo '$("document").ready( function () {';
echo 'alert($("a", $("' . $info . '")).html());';
echo '});';
echo '</script>';
?>
The above doesn't alert anything. Putting in
echo 'alert("yes")';
does work, so I'm guessing there's something basic wrong with my syntax, but 4 hours later still unable to find it! :)
Thanks in advance.
You aren't Javascript-escaping the quotes in your string.
Your code creates Javascript that looks like
$("<li>...<a href="http..."...")
The quotes in the attribute end the Javascript string, creating a syntax error.
You need to call json_encode.
SLaks has the rest of your problem. But also, it's not:
$("document").ready();
It's:
$(document).ready();
The former is a selector for a tag named <document>.
This should work the way you want it to:
<?php
$info = '<li><strong>I want this text</strong></li>';
?>
<script type="text/javascript">
$(document).ready( function () {
alert($("a", $("<?php echo $info; ?>")).html());
});
</script>
You are not closing your li Tag
$info = '<li><strong>I want this text</strong>';
should be
$info = '<li><strong>I want this text</strong></li>';
You should escape info. It's breaking because you've got double quotes inside of double quotes:
$info = addslashes($info);
or
$info = json_encode($info);
or just
$info = str_replace('"', '\\"');
Try to save the HTML is a JS variable first, then use it. Also, heredocs are your friend.
<?php
$info = '<li><strong>I want this text</strong></li>';
echo <<<END
<script type="text/javascript">
$(function(){
var HTML = '$info';
alert($('a', $(HTML)).html());
});
</script>
END;
?>
echo '<script type="text/javascript">
$(document).ready( function () {
var info = \''.$info.'\';
$("a").html(info);
alert(info);
});
</script>';

PHP / MySQL / JSON encoding help

I am trying to implement the auto-complete script from http://www.devbridge.com/projects/autocomplete/jquery/. It's asking for JSON output like:
{
query:'Li',
suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania']
}
I am using PHP/MySQL. My query to get the suggestions would be something like...
<?
$drug = $_GET['drug'];
$query = mysql_query("SELECT * FROM tags_drugs WHERE drug_name LIKE '$drug%'");
while ($query_row = mysql_fetch_array($query))
{
$drug_name = $query_row['drug_name'];
}
?>
This is where I'm stuck. How do I put the array $drug_name in the suggestions and encode it for json? Thanks in advance!
Use
$drug_name[] = $query_row['drug_name'];
instead of $drug_name = $query_row['drug_name'];
Then use
?>
<script type="text/javascript">
var drugName = <?php echo json_encode($drug_name);?>
</script>
Use this grugName variable in your JavaScript.
json_encode($drug_name)
From PHP.net - json_encode

Categories