PHP String Manipulation printing function onclick adding " - php

HTML
<button class="btn btn-danger" onclick="loadPlayer('28JVGV92L'," 0)="">RepeaterCreeper</button>
PHP
echo "<button class='btn btn-danger' onclick=loadPlayer('{$caller['playerTag']}', {$i})>" . $caller['username'] . "</button>";
I have no clue why the output given is loadPlayer('28JVGV92L', " 0)="" what I was expecting the output to be was loadPlayer('28JVGV92L', 0) as there is 2 parameter for the function loadPlayer. I'm apparently not understanding how PHP manages strings and why it adds " whenever I don't even say to add it.
If you guys could reference me to a place where I could learn whatever topic this is called that'd be helpful. I've looked into PHP String Manipulation but I really just don't know why it's adding " and the extra ="" to the echoed statement.

Your quotes are off, causing the browser to think $i is intended to be an attribute of the button, and adds the ="". The only way that makes sense is if there is a quote in the value of $i. be careful how you define your variables.

Related

how to add single quote inside the php when passing argument for onclick javascript function?

Anyone know how should I write to get long hash value inside the single quote but when writing this code inside the php.
For example at the moment my code is below which I know its wrong:
$raw_tbody .= "<td width='10%'><a onclick='customRuleEdit('{$row['hash']}')' >";
So when I write like above its display on HTML side like below: (Which is wrong as you can see the single and double quotes)
<a onclick="customRuleEdit(" f6710ecaa686546bb424525686c792823f8d5193')'="">
What I want is as below Output:
<a onclick="customRuleEdit('f6710ecaa686546bb424525686c792823f8d5193')">
So anyone knows what I should write to make the output like above??
Thanks
Try this code for helpful..
$raw_tbody .= "<td width='10%'><a onclick=\"customRuleEdit('{$row['hash']}')\" >";

Input field being rendered as plain text

Im using PHP to render a page. I have echo statements such as
echo "<input type=\"text\" name=\"numPlate\" id=\"numPlate\" onblur=\"caps(this.id);\" $focusPlate required value=\"" . isset($_POST['numPlate']) ? $_POST['numPlate'] : "" ."\">";
but the value is being output as plain text rather than in the input field. If i take the ternary out then the input field is being rendered, which tells me that causes the issue, but i really don't understand why. The problem is is that I have about 20 other fields on this page in a similar fashion, so it's not a very clean solution to evaluate each of these statements to a variable and have the respective variables input into the value property.
Are there any other ways I could achieve this? Thanks
Edit:
Rendered html:
<div class="row"><label for="plate">Rendszám*</label>GMS245</div>
As you can see the string is just a text.
I tested it out and I believe my second suggestion could solve it. Wrap the ternary in parenthesis. From my test, it was trying to evaluate the true response to the ternary instead of setting it as the value or ignoring it. I also replaced the inner quotes with a single quote to clean up the code while still allowing the variables to be evaluated.
echo "<input type='text' name='numPlate' id='numPlate onblur='caps(this.id);' $focusPlate required value='" . (isset($_POST['numPlate']) ? $_POST['numPlate'] : "") ."'>";

Prevent html entity conversion php

I have a form with a url input that I need to prevent from converting, so that I can use $_GET on the target page. I have tried urlencode, urldecode, html_entity_decode, etc, but none of it prevents the html entity conversion (parse_url did nothing but get rid of all the important stuff). This is the only thread I have found that comes close to what I am trying to achieve.
It seems like there should be a simple solution, and this is not happening anywhere else I am using a url like this...
Thanks to anyone who can help!
echo "<option value='seeArtist.php?aid=".$row[0]."&ac=".$row[1]."&img=".$row[2]."'">
(blah, blah)
<input type="submit" style="margin-left:10px" name="submit" value="Go" />';
This is the result from clicking the submit button.
seeArtist.php?art_con=seeArtist.php%3Faid%3D18%26not%3Bac%3D+(aka)+Banksy%26not%3Bimg%3D0&submit=Go
Two variables are integers, so the database content is not url-encoded.
I suspect that since this is not happening anywhere else, and this is the only place where I am putting a link in a select option, that it has something to do with the submit action. In firebug the link shows up exactly the way it is supposed to. When I submit the url gets encoded.
Regardless of the PHP, your HTML is incorrect. You need to encode the ampersands. Your code should resemble this:
echo "<option value=\"seeArtist.php?aid=" . $row[0] . "&ac=" . $row[1] . "&img=" . $row[2] . "\">\r\n";
I also took the liberty of converting single-quotes to escaped double-quotes.

PHP echo changing text?

I'm a beginner PHP programmer, and I was wondering what was wrong with my code.
Here is the small excerpt from the affected spot:
echo "<form action='?tab=4' name='toedit5' method='get'><input value='text' onblur='edit('toedit5')' /></form>";
In Chrome's Developer Tools, the form element totally disappears, and the edit('toedit5') becomes edit(' toedit5').
The edit() function doesn't execute.
Is there anything wrong with this one line of code? Otherwise it is outside code messing with it. Sorry I didn't include it, but I don't know what to include. If you need more information, please tell me.
Thanks.
You need to escape your quotes inside your quoted echo'd statement, like this:
<?php
echo "<form action='?tab=4' name='toedit5' method='get'>";
echo "<input value='text' onblur='edit(\"toedit5\")' />"; // escaped..!
echo "</form>";
?>
It helped me to think about it like this when I was starting out: how does your browser know if the second single quote in onblur='edit('toedit5')' is closing your onblur statement or opening up the parameter? In this example, your browser will pair up the first 2 quotes it sees and assign that to the onblur attribute, i.e.: onblur='edit(' only!
Update 1:
Using the code above, I inspected a quick PHP page I created in Chrome's developer tools and was able to see the following (form available for inspection):
You really should use the more standard double quotes around the HTML properties and use single quotes around your string, with escaped single quotes within the javascript method calls. Like this:
echo '<form action="?tab=4" name="toedit5" method="get"><input value="text" onblur="edit(\'toedit5\')" /></form>';

Error in passing argument through a function.

I am having this error in my code..
echo "<input type='button' value='send mails' onclick=\"sendmails(".$sendQuestion.")\">";
i kind of feel stupid posting this question. But I am unable to figure out what's wrong in this code
This code is written in php. it works fine if I don't pass any arguments in the sendmails function, but when I pass argument it gives error as:
missing ) after argument list
sendmails(Type your Question here testin to post from chrome)
Add a quote around it:
echo "<input type='button' value='send mails' onclick=\"sendmails('".$sendQuestion."')\">";
Without the quotes, javascript misreads your string.
If you take a look at the generated HTML, it'll look something like this :
<input type='button' value='send mails' onclick="sendmails(Type your Question here testin to post from chrome)">
There are some quotes missing arround the string you're passing as parameter to the JS function sendmails
So, I'd say add some quotes arround it ; a bit like this :
echo "<input type='button' value='send mails' onclick=\"sendmails('".$sendQuestion."')\">";
EDIT : added more stuff...
But, if $sendQuestion contains quotes, it'll get you another error... So it might be usefull to
bad idea : either replace those ' with \' with something like str_replace
or "transform" the string to a valid-JS one, with, for instance, json_encode
The second solution will get you a PHP code like this one (note that json_encode adds double-quotes arround the string... so it becomes harder to embed directly in the function call... so let's use a variable) :
$sendQuestion = "Type your Question' here testin to post from chrome";
$json = json_encode($sendQuestion);
echo '<script type="text/javascript">' . "\n";
echo 'var myString = ' . $json . ';' . "\n";
echo '</script>' . "\n";
echo "<input type='button' value='send mails' onclick=\"sendmails(myString)\">";
And the generated HTML will be :
<script type="text/javascript">
var myString = "Type your Question' here testin to post from chrome";
</script>
<input type='button' value='send mails' onclick="sendmails(myString)">
Which is much more nice :-)
Maybe not perfect yet... But, by now, I think you get the point ;-)
As a sidenote : json_encode only exists since PHP 5.2... so you might want to check the version of PHP you are using...
The first thing you are doing wrong is posting some PHP code when the error is being reported by your JavaScript engine.
To debug it, the JavaScript is important. We can guess what the JS might look like, but it is much easier if we can see the JS to start with.
In this case, the most probable explanation is that you want to write some JS that has a string literal in it — but you aren't surrounding the JS argument with quote marks.
Think of PHP as a templating engine. Try this:
?>
<input
type="button"
value="Send mails"
onClick="sendmails('<?php echo $sendQuestion; ?>');">
<?php

Categories