views:

29

answers:

2

I am building some html in a php script in order to send it back to a page via Ajax.

$other_content = Model_Db::dbSelect($query);

        $div_center .= "<table>";
        for($i = 0;$i<sizeof($other_content);$i++) {
            $div_center .= "<tr>";

            $div_center .= "<td><a href='#' onclick='changediv('test','0')'>".$other_content[$i]->content_name."</a></td>";
            $temp = "<td><a href='#' onclick='changediv('test','0')'>".$other_content[$i]->content_name."</a></td>";
            die($temp);

            $div_center .= "</tr>";
        }
        $div_center .= "</table>";

As you can see I am doing a die() to see the created string. My ouput should be something like: <a href="#" onclick="changediv(" test','0')'>Content Name</a>

But instead I get: <a href="#" onclick="changediv(" test','0')'="">Content Name</a> I do not understand where this ="" comes from after my onclick declaration...

Can anybody see what's wrong here? I am a bit puzzled as I really don't see where it could come from!

Cheers

+1  A: 

I would suggest you to escape the quote characters

$temp = "<td><a href=\"#\" onclick=\"changediv('test', '0')\">" . 
$other_content[$i]->content_name. "</a></td>";

The \" escapes the double quoute in a string

Tobias
It worked but I still don't really understand why not escaping quotes would add ="" .... Anyway, thanks for your help!
Piero
A: 

You messed up the function interpretation of single-quote marks as double-quotes:

yours <a href='#' onclick='changediv('test','0')'>

func  <a href="#" onclick="changediv(" test','0')'="">

It assumes this (between % signs) %test','0')'=""% is a parameter of your tag, try substituting single-quotes with double-quotes and make it html/xhtml compliant:

<a href="#" onclick="changediv('test','0')">

So single-quotes and double-quotes will be correctly set.

You have to change PHP quotes too

Lex