1

DEscription :

I have a php script that displays the div on an html page

echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id)"></div>';

Now when I click on this div the star_it function gets called and it works perfect...

The problem

I want to pass another argument in the star it function .. like this

echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id,'.$each_status['regno'].')"></div>';

or a simple alphabet if any like star_it(this.id,s)

but when I do so the function stops working as when I click the div the function does not gets called ....

I dont know why I am stuck and now my star rating system does not work... I have absolutely no idea what is wrong

Anyone ??

7
  • Can you show us the code in your star_it function? Commented Oct 24, 2014 at 17:35
  • 1
    if the second attribute is a string or letter, add quotes around the parameter. otherwhise your php script will produce invalid syntax javascript. Commented Oct 24, 2014 at 17:35
  • 1
    What's the resulting markup of the second echo? Are you getting any JS errors? perhaps $each_status['regno'] is empty. Commented Oct 24, 2014 at 17:35
  • @BrianDriscoll thats not the point the star_it() if you really want to know should just alert me with two values passed for now .... it just doesn't do any this ... if I pass this.id it works good but as soon as the second argument comes I cant call the function Commented Oct 24, 2014 at 17:40
  • Does the star_it function accept a second argument? If it's not running at all, you're getting an error. Check your javascript console. Commented Oct 24, 2014 at 17:41

3 Answers 3

3

You should modify your string quote's like this :

<?php 
$status = "second test";
echo '<div class="star_box" id="'.$id.'" onmousedown="star_it(this.id,'."'".$status."'".');" >Hello</div>';
?>

<script>
    function star_it(foo1,foo2){
        console.log(foo1);
        console.log(foo2);
    }
</script>

This example works for me.

So with your code :

<?php 
echo '<div class="star_box" id="'.$id.'" onmousedown="star_it(this.id,'."'".$each_status['regno']."'".');" >Hello</div>';
?>
Sign up to request clarification or add additional context in comments.

2 Comments

no. that's not "escaping". you're just quoting. for PHP->javascript insertion, you should ALWAYS use echo json_encode($var)
You sir are a life saver :) ... I am not really familiar with escaping strings quotes ... thankyou so much :)
2

Here you go

echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id,\''.$each_status['regno'].'\')"></div>';

you forget to add \' before and after your string , the JS engine on the browser will treat your string as an undefined variable and the function will not work because of that .

//my English is not so good.

Comments

-1

Try

echo '<div class = "star_box" id = "'.$each_status['post_id'].'" onmousedown = "star_it(this.id,\"'.$each_status['regno'].'\")"></div>';

Comments

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.