php pre-populating text area - php

The following php code works for prepopulating an input box, but doesn't work for a text area. The issue is that the standard input box is small in height so the text is displayed on one line. I tried to use the text area instead but it appears blank when rendered. The code shows both methods, the working method, followed by the text area method.
Can anyone advise?
<?php
echo "<form method='POST' action='edit.php'>
<fieldset>
<div class='form-group'>
<label for='descriptionField'>Description</label>
**<input type='text' value='$description' class='form-control' name ='nameField'>**
**<textarea class='form-control'value='$description' input type='text' rows='10' name ='descriptionField'></textarea>**
<input type='hidden' value='$id_data' name='idField'>
<br>
<input class='btn btn-success' type='submit' value='update'>
</div>
</fieldset>
</form>";
}
?>

Did you try like this?
<?php
echo "<form method='POST' action='edit.php'>
<fieldset>
<div class='form-group'>
<label for='descriptionField'>Description</label>
**<input type='text' value='$description' class='form-control' name ='nameField'>**
**<textarea class='form-control' input type='text' rows='10' name ='descriptionField'>htmlspecialchars($description);</textarea>**
<input type='hidden' value='$id_data' name='idField'>
<br>
<input class='btn btn-success' type='submit' value='update'>
</div>
</fieldset>
</form>";
}
?>

If you want to set a text areas input, you can simply put in in between the <textarea> tags like so in your example:
<textarea class='form-control' input type='text' rows='10' name ='descriptionField'>
$description
</textarea>
Also refer to the w3schools article on the textarea tag for further details.

Related

Trying to change this from an image to a button, when I change it to look like a button I lose functionality

I am losing functionality when I change from an image to a simple button. This is the code when it is an image and it submits:
$form= "
<form method='post' action='/login.php'>
<b>Name:</b><br><input type='text' name='name'
size='19' class='login'><br><b>Password:</b><br><input
type='password' name='password' size='19' class=
'login'><br>
<img src='/layout/blank.gif' height='6' width='1'
alt='blank'><br>
<input type='hidden' name='redir' value='$redir'>
<input type='image' src='/layout/loginword.gif' name=
'action' value='Go' style='vertical-align: middle;'
title='Login'> (<a href='/lostdetails.php' class=
'menu'>Lost Details?</a>)
</form>";
When edited to display a button instead of an image as below I lose the functionality:
$form= "
<form method='post' action='/login.php'>
<b>Name:</b><br><input type='text' name='name'
size='19' class='login'><br><b>Password:</b><br><input
type='password' name='password' size='19' class=
'login'><br>
<img src='/layout/blank.gif' height='6' width='1'
alt='blank'><br>
<input type='hidden' name='redir' value='$redir'>
<input type='button' name=
'action' value='Go' style='vertical-align: middle;'
title='Login'> (<a href='/lostdetails.php' class=
'menu'>Lost Details?</a>)
</form>";
This is occuring because input type="image" has the same functionality of a submit button, while input type="button" does not have any implicit functionality. (It is just a dumb clickable button)
You need to change it to input type="submit"
Don't use type="button" in the submitting input! Try to use type="submit" in that "Go" field.
That's because the first attribute value prompt to the browser that the button is literally a normal object which doesn't have nothing to do with that form. Maybe you can use it as a link, or a Javascript event caller ... but not as the submitting action.
So the new code will be:
<form method='post' action='/login.php'>
<b>Name:</b><br><input type='text' name='name'
size='19' class='login'><br><b>Password:</b><br><input
type='password' name='password' size='19' class=
'login'><br>
<img src='/layout/blank.gif' height='6' width='1'
alt='blank'><br>
<input type='hidden' name='redir' value='$redir'>
<input type='submit' name=
'action' value='Go' style='vertical-align: middle;'
title='Login'> (<a href='/lostdetails.php' class=
'menu'>Lost Details?</a>)
</form>
Obviously you can put it in you PHP variable, your choice. If you want to learn more about HTML buttons, here you can see the W3Schools documentation about it.
Andrea
you should use type="submit"
and if you want to use the button,
you should use the button tag
with type="submit"

reset form php value back to original value php

how to reset value with jquery form..my input text value with php. when I edit text and I reset, then back again to the value php..I've tried with jquery reset but input text
lost value.
<form>
<input type='text' id='chk' value='<?php echo $item.$i?>'/><br/>
<input type='text' value='<?php echo $item.$i?>'/><br/>
<input type='text' value='<?php echo $item.$i?>'/><br/>
<input type='reset' id='reset_button' value='reset'/>
</form>
when i edit input text and then i click reset.input text losing value?how to input text back to original value?
thanks..
You could use a form reset button: <input type='reset' value='Reset'/>
If you don't want the button to be visible, you could hide the button and call the click() method to reset the form.
Example:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>With button:</h2>
<form>
<input type='text' value='Foo'/><br/>
<input type='text' value='Bar'/><br/>
<input type='reset' value='Reset'/>
</form>
<br/>
<br/>
<h2>Without button:</h2>
<form>
<input type='text' id='chk' value='1'/><br/>
<input type='text' value='Foo'/><br/>
<input type='text' value='Bar'/><br/>
<input type='reset' id='reset_button' style='display: none;'/>
<input type='submit' onclick="if ($('#chk').val() == '0') $('#reset_button').click(); return false;" value='Check if 1 (reset if 0)'/>
</form>

PHP contact form with custom answer based on checkbox

I'm trying to build a php contact form (to be used into a wordpress custom page) that show a different "thank you message" based on the checkbox selection.
There will be three checkbox, every checkbox in used will make appear a download link under the "thank you" message.
<form method="post" action="php/contact-send.php" id="contact_form">
<p><label class="form_label" for='form_name'>Name</label>
<input id='form_name' type='text' name='name' class="textbox" value='' /></p>
<p><label class="form_label" for='form_email'>E-mail</label>
<input id='form_email' type='text' name='email' class="textbox" value='' /></p>
<p>Download1 <input id='form_name' type='checkbox' name='Download1' class="textbox" value='Download1' /></p>
<input id='form_submit' type='submit' name='submit' value='Send message' />
<!-- To Email -->
<input id='to_email' type='hidden' name='to_email' value='<?php echo "";?>' />
<!-- hidden input for basic spam protection -->
<div class='hide'>
<label for='spamCheck'>Do not fill out this field</label>
<input id="spamCheck" name='spam_check' type='text' value='' />
</div>
</form>
<!-- contact form ends here-->
<!-- This div will be shown if mail was sent successfully -->
<div class="hide success">
<p><?php echo thanks_message;?></p>
<p>
<?php
if(isset($_POST['Download1'])) {
// The checkbox was enabled...
echo "Link download 1";}
else { echo "much wrong, wow";}
?>
</p>
The problem is i can't get it to work and detect when the first checkbox is selected, it alway give me the else message even if i select it. I'm kinda new with php and i don't really get what i'm doing wrong,
Thanks in advance for any help

Removing the variables in url

Before I am told to look at other posts that are similar, please note that I have tried to apply them but to no avail.
I need to clear the variables in the url after a series of if statements are true. I have the following code:
<?php
$_SESSION['passlevel']='0';
if ($_SESSION['passlevel'] == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$_SESSION['passlevel'] = '1';
// add something that removes url string
}
else
{
echo "you didn't select purple.";
}
}
else echo "login with your colorlock passcode.";
}
if ($_SESSION['passlevel'] == '1')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#DD7500")
{
echo "you have passed step two.";
$_SESSION['passlevel']='2';
// add something that removes url string
}
else
{
echo "you didn't select orange.";
}
}
else echo "enter remaining colors.";
}
?>
This is for color dependent password that has the background of each of 10 divs change to one of 10 colors after a div is clicked on (note that some of the echoes are only there for now as a method of debugging). The information is sent to the url through multiple forms:
<form>
<div id="box1" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box1color ?>'/>
<input type='hidden' name='name' value='box1color'/>
</form>
<form>
<div id="box2" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box2color ?>'/>
<input type='hidden' name='name' value='box2color'/>
</form>
<form>
<div id="box3" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box3color ?>'/>
<input type='hidden' name='name' value='box3color'/>
</form>
<form>
<div id="box4" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box4color ?>'/>
<input type='hidden' name='name' value='box4color'/>
</form>
<form>
<div id="box5" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box5color ?>'/>
<input type='hidden' name='name' value='box5color'/>
</form>
<form>
<div id="box6" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box6color ?>'/>
<input type='hidden' name='name' value='box6color'/>
</form>
<form>
<div id="box7" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box7color ?>'/>
<input type='hidden' name='name' value='box7color'/>
</form>
<form>
<div id="box8" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box8color ?>'/>
<input type='hidden' name='name' value='box8color'/>
</form>
<form>
<div id="box9" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box9color ?>'/>
<input type='hidden' name='name' value='box9color'/>
</form>
<form>
<div id="box10" class="box" onclick='this.parentNode.submit()'></div>
<input type='hidden' name='box_1_color' value='<?php echo $box10color ?>'/>
<input type='hidden' name='name' value='box10color'/>
</form>
An example of the url after one of the colors (purple) is selected:
http://localhost/colorlock/index.php?box_1_color=%23800080&name=box9color
So I want to remove the ?box_1_color=%23800080&name=box9color part. How could i remove it where the code says "// add something that removes url string" so that when the next block of code is initiated it doesnt run immediately with the same color code in the url before a user can select their next color?
I hope my question is clear (i understand that it a confusing) and i hope that it is answerable. Thank you.
You need to do one/some of 3 things:
Use POST. This is a good idea but it would still be best combined with...
Redirect to the same page without the URL arguments after processing the form.
Use ajax to process the user input.
The easiest of these to integrate with your existing code is probably a combination of 1 and 2. To do this, add these lines where your comment is:
header("HTTP/1.1 303 See Other");
header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}");
exit;
...and change all the <form> elements in your HTML so that they read:
<form method="post">
...and obviously change all occurrences of $_GET in your PHP code to $_POST
Could you use POST instead of GET? You use $_POST in your PHP instead of $_GET and you never have to worry about what's in the URL - it's always clean.
Try
unset($_GET['box_1_color']);
in aproriate place of your code

PHP, select a string between two strings, then get one value from the selected string

I have a file, with many parts of code.
Each part starts with <form> and ends with </form>.
Between each form, there is something like this: <input type='text' name='date' value='2010-01-01'><input type='text' name='username' value='test'>
I want to parse each part of the code with php and get the value of input username, where the value for input date is '2010-01-01'
Any solutions please?
example:
<form>
<input type='text' name='date' value='2010-01-01'>
<input type='text' name='username' value='test'> <!-- How can I get this value ('test')? -->
</form>
<form>
<input type='text' name='date' value='2010-01-02'>
<input type='text' name='username' value='test2'>
</form>
.
.
.
preg_match('/form.*name=\'date\' value=\'2010-01-01\'.*name=\'username\' value=\'(.*)\'/Usim', $f, $r);
printf($r[1]);

Categories