I want to have a text-field (input type="text") or text-area in html that takes users input. After "submit" clicked, PHP returns results. I want to repopulate the text-field or text-area with original user input. However my code only works with text-field, but not text-area:
This works:
<INPUT TYPE = "text" NAME = "seqbox" SIZE = 50 PLACEHOLDER = "Enter sequence here" VALUE = "<?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?>">
This does not work:
<TEXTAREA NAME = "seqbox" COLS=100 ROWS=20 PLACEHOLDER = "Enter sequence here" VALUE = "<?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?>"></TEXTAREA>
Any idea why? Thanks!
Textarea doesn't have a value property. You need to set it in between the element like this:
<TEXTAREA NAME = "seqbox" COLS=100 ROWS=20 PLACEHOLDER = "Enter sequence here">
<?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?>
</TEXTAREA>
Textareas don't take a value attribute.
<TEXTAREA NAME = "seqbox" COLS=100 ROWS=20 PLACEHOLDER = "Enter sequence here"><?php if(isset($_GET['seqbox'])) {echo $_GET['seqbox'];} ?></TEXTAREA>
A note: you should run $_GET['seqbox'] through htmlspecialchars or malicious users will be able to inject things like JavaScript into your page through a specially crafted URL (an XSS vulnerability).
Related
Do you have any idea how to write text in two inputs at the same time, what is written in input 1 appears in input 2 but modified for example:
hello do you have any idea how to write text in two inputs at the same time, what is written in input 1 appears in input 2 but modified for example:
Input 1: this is a text
input 2: this-is-a-text
I try to use str_replace () but I can't do it in real time
<?php
$texto = $_POST['title'];
$urlcambiado = str_replace(" ", "-", $texto);
?>
<input type="text" name="title" class="form-control" placeholder="Ejemplo: sword-art-online">
<input type="text" name="url_code" class="form-control" placeholder="Ejemplo: sword-art-online">
This will do what you want using pure javascript:
function URLChange(titlestr) {
var url=titlestr.replace(/ /g,"-");
document.getElementsByName("url_code")[0].value=url;
}
<input type="text" name="title" class="form-control" placeholder="Ejemplo: sword-art-online" onkeyup='URLChange(this.value);'>
<input type="text" name="url_code" class="form-control" placeholder="Ejemplo: sword-art-online">
This has to be done on client-side using JavaScript and goes a little bit like this:
Reference the two input fields
Both have it's name attribute set to title and url_code respectively. To get a reference to it we can use the getElementsByName() method which returns a HTMLCollection - an array. Since there's just one element for each name we can append a [0] to get the first element in the array.
var firstInput=document.getElementsByName("title")[0];
var secondInput=document.getElementsByName("url_code")[0];
Attach an input event listener
To find out if the user has typed anything into the first input we need to use this listener which invokes a callback function we can then use to get the actual text.
firstInput.addEventListener("input",process);
Modify the text inside the second input
Inside the callback function we can retrieve the text from the first input, use a regular expression to replace whitespaces by a minus sign and assign the text to the second text field.
function process(e) {
secondInput.value = e.target.value.replace(/\s/g, '-');
}
Here's a complete example:
var firstInput = document.getElementsByName("title")[0];
var secondInput = document.getElementsByName("url_code")[0];
function process(e) {
secondInput.value = e.target.value.replace(/\s/g, '-');
}
firstInput.addEventListener("input", process);
<input type="text" name="title" class="form-control" placeholder="Ejemplo: sword-art-online">
<input type="text" name="url_code" class="form-control" placeholder="Ejemplo: sword-art-online">
I am trying to allow users to edit their biography and on the page I want the editable text from the DB to appear in the text box. How do I do this? Currently I have the text as a placeholder, but I want to make that editable.
Also for other, shorter fields like a users company name, when I insert the value as the placeholder (I don't want it to be editable like the bio so it doesn't resubmit every time), it can't display more than one word. How can I fix this.
Note: I wrote a function that only displays a value from SQL if there is one, else it displays a generic text, i.e. "bio" or "email"
Here is my function where $content is something like $_POST["bio"] and
<?php
function echo_content($content,$name)
{
if(!empty($content)){
echo($content);
}
else{
echo($name);
}
}
?>
Below is my html/php where $content is a value from SQL.
<div class="form-group">
<legend>Bio: </legend><textarea rows="4" cols="50" class="form-control" name="bio"
placeholder=<?php echo_content($content[0]["bio"],"Bio");?> type="text"/></textarea>
</div>
You're dumping a string into an html attribute, WITHOUT quotes, so basically you're producing:
<textarea ... placeholder=Four Score and Seven Years ago type="text">
so your placeholder is Four, and then there's a bunch of unknown/illegal attributes, Score, and, Seven etc...
Try
<textarea ... placeholder="<?php echo htmlspecialchars($var) ?>" ...>
instead. note the " and use of htmlspecialchars() to quote out html metachars.
In other words, you're basically suffering from a self-inflicted HTML injection wound.
I have my website connected to a database with MySQL and I am echoing out the data but it doesn't display information after a space. E.g. in the database 'Hello there' would just be 'Hello'.
I know this is quite a common question asked but I just couldn't get mine working. An example is this:
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
You're missing quotes around your HTML attribute value. Without it the first word is considered the value and everything else is considered a new HTML attribute.
County: <input type = "text" name = "county" value = <?php echo $row["county"]; ?>> <br><br>
Should be:
County: <input type = "text" name = "county" value = "<?php echo $row["county"]; ?>"> <br><br>
//^ ^
I'm making a query to the database and am showing the value in input type text as follows:
<input type='text' name='title' value="<?php echo $noticia->_title; ?>" />
What happens is that if the text coming from the database comes within "" the text does not appear because the " " of value. If I switch to '' have the same problem if the text coming from the database is inside ''. How can I solve this problem?
value="<?php echo htmlspecialchars($noticia->_title) ?>"
htmlspecialchars() will encode any HTML metacharcters in there that would otherwise break your form, e.g.
$title = 'Hello "Joe"';
<input ... value="Hello "Joe"" />
^---breaks the form
becomes
$title = htmlspecialchars('Hello "Joe"');
<input ... value="Hello "Joe"" />
Convert text to HTML with htmlspecialchars.
echo htmlspecialchars($noticia->_title);
I have created a form, with 5 textbox fields and I want to add those five entries in the database. I want to use the textbox "array", that way I can use a for-each when saving to the database. As anyone, any code on how to do this or can direct me in the right path?
input type="text" value="whateva" name= ?php text[0] ?>
input type="text" value="whateva" name= ?php text[1] ?>
input type="text" value="whateva" name= ?php text[2] ?>
if (isset($_POST['Submit']) {
//add to db
(for-each $text as $val) {
//add to db
}
}
Is this possible?
HTML
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
<input type="text" value="whateva" name="text[]" />
PHP
if (!empty($_POST['text'])) {
foreach ($_POST['text'] AS $value) {
// add to the database
$sql = 'INSERT INTO tableName SET fieldName = "' . mysql_real_escape_string($value) . '"';
}
}
Yes, HTML supports arrays. just name your textareas like this:
<textarea name="field[]"></textarea> /* Notice square brackets */
For this example, in PHP, your $_GET or $_POST will have array key with name 'field' and values from these textareas.
If 'Submit' is the name of the submit button. yeah that will work.
but few suggestions:
correct it as:
< input type="text" value="whateva" name= "" />
Use validation for the text submitted by user
IMPORTANT: "GET A BOOK ON PHP" and learn it. Seriously, if you learn this way, you wont become a good programmer. You are learning it the hardway. Book is must for you.