Reputation: 45
I have this loop which generates links for a dropdown.
$count = count($this->contents);
for($i = 0; $i < $count; $i++)
{
$this->ret .= "<a onclick='changeFDD(" . strval($this->contents[$i]) . ")'>" . $this->contents[$i] . "</a>";
}
This produces the following list
<div id="fddContentID" class="fddContent">
<a onclick="changeFDD(Mr.)">Mr.</a>
<a onclick="changeFDD(Mrs.)">Mrs.</a>
<a onclick="changeFDD(Ms.)">Ms.</a>
</div>
The issue is that I need the parameters to be strings. I would imagine that the strval would of accomplished this.
When I just try to insert single quotes it throws the structure off a little bit.
PHP with single quotes
$this->ret .= "<a onclick='changeFDD('" . strval($this->contents[$i]) . "')'>" . $this->contents[$i] . "</a>";
Returned HTML Result
<div id="fddContentID" class="fddContent">
<a onclick="changeFDD(" mr.')'="">Mr.</a>
<a onclick="changeFDD(" mrs.')'="">Mrs.</a>
<a onclick="changeFDD(" ms.')'="">Ms.</a>
</div>
I understand why the result is the way it is, what I don't understand is how I can encapsulate that variable so the Javascript processes it correctly.
Additional Slightly More Detailed Information
I'm working on a form builder in PHP and I created a custom dropdown box using a button and a div that I control its visibility with Javascript and CSS. Within this dropdown box each item is a link.
When you click on the link it runs the Javascript function changeFDD(nVal)
function changeFDD(nVal) {
document.getElementById("fddBtnID").value = nVal;
alert("test");
}
nVal corresponds to the new value the button will have.
When I generate this dropdown box, a method which iterates through an array displays these links the only issue is I'm having trouble encapsulating the variable with quotation since there are already so many quotations in that area. I tried a few functions like strval and casting it as a string but results stayed the same.
Here is the layout of the dropdown
<div class="FDDWrap">
<button onclick="fddDrop()" id="fddBtnID" class="fddBtn">Mr.</button>
<div id="fddContentID" class="fddContent">
<a onclick="changeFDD(Mr.)">Mr.</a>
<a onclick="changeFDD(Mrs.)">Mrs.</a>
<a onclick="changeFDD(Ms.)">Ms.</a></div>
</div>
Any and all guidance or criticism is appreciated. If you see any bad habits potentially forming please don't hesitate to roast me.
Upvotes: 1
Views: 1391
Reputation: 45
As bishop suggested json_encode was a good way to accomplish this.
Instead of
$this->ret .= "<a onclick='changeFDD(" . strval($this->contents[$i]) . ")'>" . $this->contents[$i] . "</a>";
json_encode did the trick easy
$this->ret .= "<a onclick='changeFDD(" . json_encode($this->contents[$i]) . ")'>" . $this->contents[$i] . "</a>";
Upvotes: 1
Reputation: 331
U need to remember about escaping special characters with \
$ret .= '<a onclick=\'changeFDD(\'' . $contents[$i] . '\')>' . $contents[$i] . "</a>";
or if u want to use double quotes
$ret .= "<a onclick='changeFDD('{$contents[$i]}')'>{$contents[$i]}</a>";
Both will return the same results.
Upvotes: 1