Displaying punctuation in a button's data attribute - php

I'm trying to put a PHP variable in a button data attribute however weird characters are showing up instead of the punctuation. As an example instead of an apostrophe ' is showing up instead. The variable is normal everywhere else on the page. How can I fix this?
<button id="coolButton"data-name="<?PHP echo $name;?>"

You need an space between the final of id and the beginning of data-name
<button id="coolButton" data-name="<?PHP echo $name;?>"

You have the quotes at the wrong place
Instead of this:
<button id="coolButton"data-name="<?PHP echo $name;?>"
Try this:
<button id="coolButton" data-name="<?PHP echo $name; ?>">
You also didn't close the button tag, only the php part.
Sorry for my bad english

Related

Display iframe value at textbox in php

I have insert iframe value into my database using php. I need to display the inserted value in a textbox in another page. But while displaying the data it only displays the value till the first " in the iframe.
my code
<label>Location</label> <input type="text" style="resize:vertical" class="md-input" name="locationmap" class="md-input" value="<?php echo $comp_row['comp_locationmap']?>">
<i class="glyphicon glyphicon-pushpin form-control-feedback glyphiconalign"></i> </div>
The output textbox only displays "<iframe src= "
you need to escape the special characters!
you can do this by using htmlspecialchars() to convert only special characters to HTML entities
echo htmlspecialchars($comp_row['comp_locationmap']);
or htmlentities() to convert all characters to HTML entities
echo htmlentities($comp_row['comp_locationmap']);

Input image as clickable button

I want to make an image which is clickable and in case when its clicked I use $_POST to get it's value and use it. It looks like this:
if (isset($_POST['checked'])){
$q = mysql_query("UPDATE table SET checked = 2 WHERE tr_id = ". $_POST['checked']);
}
<form action="" method="POST">
<input type="image" src="images/checked.png" name="checked" value="<?php echo $info[0]; ?" />
</form>
Well in Chrome it works, but then I realised that in IE and Mozilla it doesn't work, can anyone suggest me same thing but so it works on all browsers?
Main thing is that this images value field get it's value from database with mysql_fetch_row and I must use this value in update query.
It would be nice if you close the double quotes and the input tag:
<input type="image" src="images/checked.png" name="checked" value="<?php echo $info[0]; ?>">
You are missing double quotes " and > there at the end :D
value="<?php echo $info[0];">
It should work even in other browsers, but if you want rich HTML in your buttons, just stick with <button>:
<button>
<img src="someImg.jpg" alt="">
</button>
This will submit your <form> as well.

How do i implement htmlspecialchars for a textbox value?

Hello i'm currently using a table to input values into a custom meta field. I have a text box called episode title. My problem here is that if the characters ' " are added in the field then everything goes in to chaos. I want to use the htmlspecialchars to input the values as &quot and &#039 instead of ' ". the below code does not work to covert the characters. Can anyone please help?
<p>
<input type="text" name="episode_title[]" id="episode_title[]" value="<?php echo ($_POST['episode_title']); ?>" class="title regular-text" style="width: 98%" />
<span class="description"><?php _e('Title of The Episode.'); ?></span>
</p>
add this to the htmlspecialchars call: ENT_QUOTES like so:
<?php echo htmlspecialchars($_POST['episode_title'], ENT_QUOTES); ?>
This will enable changing of both the " and the ' quotes
$_POST['episode_title'] is an array, so you need to get the right value from the array and use htmlspecialchars() on that value.
Something like:
value="<?php echo htmlspecialchars($_POST['episode_title'][$some_key]); ?>"
Edit: I am assuming that the $_POST array contains the results of the form when it is submitted.

unable to get my window.location to work within echo tag

echo "<form><input type='button' value='$back_label' onclick='window.location="'$url'"'/></form>";
I cant figure the whole single and double quotes thing when it comes to the window.location code because it has an extra set of single quotes to wrap around the url. I have no idea what to do. I tried escaping the quotes.
Also, can you use a relative path for this method?
Thanks
Try this
echo "<form><input type='button' value='$back_label' onclick='window.location=\"$url\"'/></form>";
A working example on http://codepad.org/K7AafokT
Can you take it out of the PHP context?
<?php $url = 'http://www.yourdomain.com'; ?>
<form>
<input type='button' value='<?php echo $back_label;?>' onclick='window.location="<?php echo $url;?>"'/>
</form>
Just change the quotes at the end to onclick='window.location="$url"'/>
echo "<form><input type='button' value='$back_label'
onclick='window.location="$url"'/></form>";
I believe to meet the HTML 'standard' the almost all attributes must use double quotes and for javascript you need to encapsulate the url so:
echo '
<form>
<input type="button" value="'.$back_label.'" onclick="window.location=\''.$url.'\'" />
</form>';
EDIT
A cleaner way to code this is to use heredoc syntax as it eliminates the need for escaping:
echo <<<EOL
<form>
<input type="button" value="$back_label" onclick="window.location='$url'" />
</form>';
EOL;
UPDATE
You are able to go down a directory structure, and I just did a quick test and it appears to work going up the hierarchy as well.

How do I make my javascript character counter work inside php?

I have a javascript character counter that I use inside of a text area. It works great in normal html, but when I put the exact same code inside of a text area inside php, nothing.
Here it is in html when it works fine:
<div id="counter">
<span id="counter_airway" style="font-size:11px; color:#666666;">140 Character Limit</span>
</div>
<div id="grapvine_text">
<form name="CommentBox" method="post" action="Profile.php?id=<?php echo $prof->id; ?>">
<textarea name='airway' class='round_10px' onkeyup="limit_length(this,140,'counter_airway');"></textarea>
</form>
Here it is implemented inside my php form:
<div id="commentBoxBlog">
<form name="CommentBox" method="post" action="Profile.php?id=<?php echo $prof->id; ?>">
<?php
if($auth->id == $prof->id) {
echo "<div id='counter'>
<span id='counter_airway' style='font-size:11px; color:#666666;'>140 Character Limit</span>
</div><textarea name='airway' class='round_10px' onkeyup='limit_length(this,140,'counter_airway');'></textarea>
<input type='submit' name='commentProfileSubmit' value='Exhale' class='post'/>";
}
elseif(!$auth) {
echo "<textarea name='ProfileComment' class='round_10px' disabled>Please sign in to comment...</textarea>";
}
elseif($auth->id != $prof->id) {
echo "<textarea name='ProfileComment' class='round_10px'></textarea>
<input type='submit' name='commentProfileSubmit' value='Exhale' class='post' />";
}
?>
</form>
</div>
</div>
need to escape the quote, instead of:
onkeyup='limit_length(this,140,'counter_airway')
you can do:
onkeyup='limit_length(this,140,\"counter_airway\")'
You've got a quote nesting issue. You're surrounding the onkeyup attribute of the textarea with single quotes, but also using single quotes inside that javascript snippet. Since you're using double quotes for the PHP string, use escaped double quotes (\") within your javascript snippet.
Of course, it would be even better to separate javascript into an external file, and bind to the keyup event. You could do this easily by assigning an id to your textarea, and calling the following sometime after the DOM is ready:
var textarea = document.getElementById('myTextarea');
textarea.onkeyup = function() { limit_length(this,140,'counter_airway'); }
PHP works entirely on the server side. Your browser never sees a trace of PHP, just the HTML code generated by your PHP script. Javascript works entirely on the client side.
Whether your HTML comes from coding by hand, or a HTML script, is essentially not important. What you need to look at is the HTML that your script produced in the browser's "View Source" mode. Please post that into your question as well.
You have changed some of the double quotes to single quotes. This will cause errors, for example here:
onkeyup='limit_length(this,140,'counter_airway');'
Compare this to the original:
onkeyup="limit_length(this,140,'counter_airway');"
You need to escape the quotes rather than changing them:
onkeyup=\"limit_length(this,140,'counter_airway');\"

Categories