i have the jquery accordion tabs. within those tabs, i have a hidden form which on click, is auto-filled with certain values displayed within that tab. It displays correctly within the accordion tab but when i pass those same variables as auto-fill values within the form, only the first word gets displayed if there is more than 1 word. the variable name that i have put as the value attribute of the text box is exactly the same so i don't know why this happens.
echo "<tr><p><td>".$allCourses[$i][0]."</td> - <td>".$allCourses[$i][1]."</td>
it gets displayed correctly here. however, here, only the first word appears:
<form action='editCourse.php' method='post'>
<p id='edit-course-d' class='edit-accordion-tab'><img src='images/edit.png' title='Edit Course'/></p>
<p id='edit-course-f' style='display: none'>
Course ID: <input type='text' name='edit-course-id' value =".$allCourses[$i][0]."><br />
Course Name: <input type='text' name='edit-course-name' value=".$allCourses[$i][1]."><br />
(all this is within the echo "" in php)
You have to put quotes around the content of your value-attribute, otherwise the "other words" are interpreted as HTML-attributes. So what is rendered to the browser looks like:
<input value=hello world>
The browser interprets this as an input element which has two attributes:
value="hello"
world=""
So you have to put quotes ' or double-quotes " around the value of the attribute. Like that:
echo "
<form action='editCourse.php' method='post'>
<p id='edit-course-d' class='edit-accordion-tab'><img src='images/edit.png' title='Edit Course'/></p>
<p id='edit-course-f' style='display: none'>
Course ID: <input type='text' name='edit-course-id' value='".htmlspecialchars($allCourses[$i][0])."'><br />
Course Name: <input type='text' name='edit-course-name' value='".htmlspecialchars($allCourses[$i][1])."'><br />";
I use htmlspecialchars to escape the quotes that might appear inside the values (this adds another layer of safety).
Related
I'm extracting a string from XML and want to insert it as the value of an input text box. And I'm having problems with this string containing both single and double quotes:
We will have a "New Year's Eve countdown"
Here is the code I'm using to output this. I've tried using htmlspecialchars but it doesn't stop the html code breaking because of the mix of quotes in the string.
echo "<p>Info <input type='text' name='info' value='".htmlspecialchars($result->info)."' size='20' /></p>";
How can I fix this so that it correctly displays the value of the string in the text box?
You need to use the ENT_QUOTES flag to htmlspecialchars to get it to convert both double and single quotes to their html entity equivalent:
echo "<p>Info <input type='text' name='info' value='".htmlspecialchars($result->info, ENT_QUOTES)."' size='20' /></p>";
This will produce the following HTML:
<p>Info <input type='text' name='info' value='We will have a "New Year's Eve countdown"' size='20' /></p>
Which as you can see from this snippet, displays the desired string in the text input:
<p>Info <input type='text' name='info' value='We will have a "New Year's Eve countdown"' size='50' /></p>
How can I make multi line text input ?
My code outputs only the first word of any input
For example if I type in "Hello world" it will only print "Hello"
My goal is to display multi line text.
My current code is:
echo "<td><input type=text name=opis value=".$row['opis']." </td>";
I know is the correct tag , but i can't incorporate that into my code.
My code outputs only the first word of any input
For example if I type in "Hello world" it will only print "Hello"
Always wrap you attributes values in quotes. Suppose your $row['opis'] has value "some value here", so in rendered html it will be:
<td><input type=text name=opis value=some value here /></td>
So do you think browser will understand that "some value here" is data of value attribute and not a list of some other attributes?
So, use quotes with attributes' values, always:
echo '<td><input type="text" name="opis" value="' . $row['opis'] . '" /></td>';
As you can see I've changed quotes and also closed input tag ( />).
I am not sure about what you mean with a multi line text input, but in HTML that would be the textarea element.
You can use it and specify how much rows do you want it to show initially. It will normally be bigger than an input[type='text'].
Your code would be something like this:
echo '<td><textarea name="opis" rows="5">'.$row['opis'].'</textarea></td>';
Also, if you want to echo something that requires the use of " you can just do:
echo '<td class="test">'.$content.'</td>';
// or in its case the other way around
echo "<td class='test'>".$content."</td>";
Here's an example of the difference between the two elements:
<input type="text" value='Hello
World
!
I
Am
Here'>
<br><br>
<textarea rows="5">Hello
World
!
I
Am
Here</textarea>
I've a column inside my table to put Html codes, I will use this table for email templating.
I have inside my page, all the templates inside my table, with two buttons, one to remove, and another one to edit.
The edit button shows the code inside a textbox, and to do the preview I did an echo to the code column.
<div class="tempcolumn">
<div><textarea name="ai" rows="15" cols="100" name="code" placeholder="Code">
<?php echo $get_temp; ?></textarea></div>
</div>
Preview
<div class="tempcolumn">
<p><?php echo $get_temp; ?></p>
<div></div>
</div>
To recognize the code and the id i created an hidden input
<input type="hidden" name="temp_id" value="'.$val['template_id'].'">
<input type="hidden" name="temp_code" value="'.$val['text'].'">
The script is working, but when i insert inside the code column some "<" or "=" doesn't work
Is inferfering because it reads the input value like this:
<input type="hidden" name="temp_id" value=" Value here + 'random character that closes the tag' ">
Is there a easier way to do that?
Thanks
You can try;
htmlspecialchars($value)
This will convert html characters to their non-interfering cousins.
See http://docs.php.net/manual/en/function.htmlspecialchars.php as I can't post the equivalents without them becoming characters.
I am trying to transfer my website into jquery mobile and am running into an error when using:
$('#information').load('getRegister.php' , function() {
$('#preloader').hide();
});
This function gets called to load html code dynamicaly into the page without a refresh, but the code that appears does not match the css of code already in the website. You can see this in the screenshot below:
I am pretty stumped on why, the buttons and text fields do not take on the styling of the form already loaded into the page. The code the .load calls is this php file:
<?php
require ('myConnectDB.inc.php');
// echo registration form
echo "
<p class=\"center\"> </p>
<form name=\"input2\" action=\"\" onsubmit = \" return register()\" method=\"post\" data-ajax=\"false\">
<p class=\"center\"> User Name:
<input type=\"text\" name=\"user\" id=\"userRegister\" /></p>
<p class=\"center\"> </p>
<p class=\"center\"> Email:
<input type=\"text\" name=\"user\" id=\"emailRegister\" /></p>
<p class=\"center\"> </p>
<p class=\"center\"> Password: <input type=\"password\" name=\"password\" id=\"passwordRegister1\" /></p>
<p class=\"center\"> </p>
<p class=\"center\"> Re-Type Password: <input type=\"password\" name=\"password\" id=\"passwordRegister2\" /></p>
<p class=\"center\"> </p>
<p class=\"center\">
<input type=\"submit\" class=\" button blue\" name = \"login\" value=\"Register\" />
</p>
</form>";
?>
You need to trigger the create event on the newly added content.
From the Docs
If you should generate new markup client-side or load in content via
AJAX and inject it into a page, you can trigger the create event to
handle the auto-initialization for all the plugins contained within
the new markup. This can be triggered on any element (even the page
div itself), saving you the task of manually initializing each plugin
(see below).
For example, if a block of HTML markup (say a login form) was loaded
in through Ajax, trigger the create event to automatically transform
all the widgets it contains (inputs
and buttons in this case) into the enhanced versions. The code for
this scenario would be:
$( ...new markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );
In your case you will probably want to something like the following
$('#information').load('getRegister.php' , function() {
$('#preloader').hide();
$('#information').trigger("create");
});
It's probably because your new html doesn't provide the same class attributes as the original form or whatever nested div structure the original style sheet uses.
Look at the class attribute of the original text fields and match those. or look at the style sheet to see how to layout your html.
I'm trying to create a textbox that will be displayed on my website. When displayed, I'd like to show some data within the text box. Here is what I have
echo "<input type=\"text\" size=\"100\" value=\"\">";
All that shows up in the text box is <a href=
And then at the end of the text box, right after the text box I see ">
I know something must be syntactically off, just not sure what.
You must encode <, ", and > chars - they can't be embedded that way. Use:
echo '<input type="text" size="100" value="'.htmlspecialchars('').'">';
You may also use urlencode() function - see which suits you better.
One more tip - use single quotes when string contains HTML-like content. This will save you adding \" everywhere.
php_code ?>
<input type="text" size="100" value="<a href="e;<?=$url;?>"e;></a>\">
<?php
php_code
maybe this will work for you
Think of what the html would look like:
<input type="text" size="100" value="">
^
|
This is where the value attribute ends!
htmlspecialchars should solve it.
You have made some mistake. Your code will result in something like that (also visible in this jsfiddle):
<input type="text" size="100" value="">
Instead you can use something like that:
echo "<input type=\"text\" size=\"100\" value=\"<a href="$url"></a>\">";
or
echo '<input type="text" size="100" value="<a href="' . $url . '"></a>">';
to receive effect visible in this jsfiddle. Is it satisfying enough?