WhatisSober
WhatisSober

Reputation: 988

Pass Jquery + Php search back to javascript?

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><a href="javascript:dosomething('.$argument.')">'.$text.'</a></p>';

What would be good way to approach this?

Any help would be appreciated! Thanks in advance!!

Upvotes: 0

Views: 73

Answers (2)

slattman
slattman

Reputation: 88

A lot of people like using json and ajax.

http://api.jquery.com/jQuery.getJSON/

http://php.net/manual/en/function.json-encode.php

Upvotes: 1

Oliver
Oliver

Reputation: 2864

This should do the trick:

echo '<p><a href="javascript:dosomething(\''.htmlspecialchars($argument).'\')">'.$text.'</a></p>';

// Edit:

If you run into problems with special characters like öäü… you can use json_encode()

Upvotes: 2

Related Questions