0

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!!

1
  • Have you tried using Javascript/jQuery to load the PHP page? From there you simply output whatever variables you want the JS to have access to Commented Feb 23, 2013 at 2:06

2 Answers 2

2

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()

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Oliver, but it didnot work. I should have cleared something: The javascript (.js file) calls this php file(with ajax get). Do you think the javascript function is available to PHP?
oh you mean the onclick html is inserted with ajax?
Thanks again, dosomething() function calls a random.php and gets data to run which is fine. I have another php which works with user's search and I want to pass into dosomething(). Its not onclick, its on document.ready(function).
If you need a more general approach, maybe json or html5 data is for you? html5doctor.com/html5-custom-data-attributes
I will look on those. Thanks Oliver, I appreciate your time! I just joined so cannot upvote you, sorry!
1

A lot of people like using json and ajax.

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

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

1 Comment

Thanks slattman, I have used jquery json for the search and i passed the results back to HTML with php. I am trying to link those search to pass into javascript. I will look onto json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.