How do I fill in the result by default before users enter details of their data.
For example my php code is like this
<b>Address:</b> <?php echo htmlentities($dnn['you_address'], ENT_QUOTES, 'UTF-8'); ?>
If before the user fill in the form of address, how do I display the default information.
For example
Address: Address has not added
You can simply use placeholder!
<input type="text" name="you_address" placeholder="Address has not added" value="<?php echo htmlentities($dnn['you_address'], ENT_QUOTES, 'UTF-8'); ?>" />
As in this PHP Fiddle -hit "run" or F9 to execute- something like this
<?php
$defVal = 'default input';
?>
<form action="" method="POST">
<input type="text" name="test-input" value="<?php echo $defVal?>">
<input type="submit" value="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST['test-input'];
}
?>
Using Javascript or Jquery. If use PHP only you will get result after you run submit form.
If you want to showing result before run submit form. You can use Javascript like alert below.
<script>
function check(){
if(document.getElementById('address').value==''){
alert("Address has not added");
}
And in your address input give id = "address" and onkeypress="check()"
Related
I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.
My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];
i an new to php . please help me how to remember text input in PHP forms after go back to form ?
i have an rgister page and want to save text input in database , but if a text input existing in database go back to register page but remember text input .
please help me !
Save the data in your session.
Call
session_start();
in the form.php and your target.php. In target.php parse all your $_POST/$_GET Parameters and store them in the session.
$_SESSION['user_name'] = check_for_valid_name($_POST['user_name']);
in form.php just set input default to your session Variable:
<input name='user_name' value='<?= $_SESSION['user_name'] ?>' />
edit: In form.php you can also use:
value='<?php isset($_SESSION['user_name'])?(echo $_SESSION['user_name']):(echo "") ?>'
to get rid of warnings/notices of uknown index/variable.
Use PHP session
<?php
session_start();
$_SESSION['name'] = "YourSession";
// ...
?>
You can store the details in a session variable. You'll have to learn working with sessions though.
define your input field something like this.this will retain values after submission
<input id="user_firstname" type="text" name="user_firstname" value=<?= $_POST['user_firstmane'] ?> >
<form action="action.php" method="post">
<input type="text" name="inmputname" value="<?php echo $_POST['inmputname']?>">
<input type="submit">
</form>
Hope it will help you
Just try this one,
<?php
$name = NULL;
if(isset($_POST['submit'])){
$name=$_POST['inputname'];
}
?>
And in your form,
<form action="action.php" method="post">
<input type="text" name="inputname" value="<?php echo $name;?>">
<input type="submit">
</form>
I have a webpage that uses php and has a bunch of input fields in which a user enters some data and I turn the input into an SQL statement to query some database. I successfully parse the input fields and put the SQL statement together but when they click the "submit" button, all the input fields get cleared. How can I make it so these input fields don't get erase every time the user clicks "submit"?
Store them in local variables like
<?php
$name = '';
$last = '';
if(isset($_POST['post_me'])) {
$name = $_POST['post_me'];
$last = $_POST['last'];
//Process further
}
?>
<form method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="last" value="<?php echo $last; ?>" />
<input type="submit" name="post_me" />
</form>
Something like this should work.
<?
if(!isset($_POST['stackoverflow'])){
$txt = "";
} else {
$txt = $_POST['stackoverflow'];
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="" method="post">
<input type="text" name="stackoverflow" value="<?= $txt ?>">
</form>
</body>
</html>
You need to check if the POST values (assuming you're using POST) are already set when the page loads (because they have already been submitted). If they are set, echo their contents into the value attributes of the form fields, or set checked=checked etc. depending on the field types.
This has always worked for me:
<input type="text" name="somename" value="<?php echo htmlspecialchars($_POST['somename']); ?>">
The key is to store the post values in session and display it using value tag.
Example:
HTML:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="First Name"
value="<?php
if (isset($_SESSION['name'])) {
echo $_SESSION['name'];
}?>" >
<input type="submit" name="submit">
</form>
PHP:
session_start();
if (isset($_POST['submit'])) {
$name=$_POST['name']);
$_SESSION['name']=$name;
}
<?php
if(isset($_POST['submit'])) {
$decimal = $_POST['decimal'];
?>
<form action="" method="post">
<input type="number" name="decimal" required value="<?php echo (isset($decimal)) ? $decimal: ''?>">
</form>
Just use your $_POST['decimal'] into your input value
Have you used sessions and Cookies in PHP??
You can store the values in session on form submit before updating in Database for a user, then check if there is a session for user and value is set for field output the value else output null.
You just have to make sure the HTML input value has a php echo statement of the appropriate php variable for that field.
Simplest answer
`<form method="post" onsubmit="return false">`
coding your input with PHP is dangerous even if with htmlentities - hope this helps
I need to get result where PHP generated ID for current button click is displayed in html textfield...
Is there any easy way of doing this?
This is the link of what I am trying to do http://dtech.id.lv/lat.php
When user first comes he doesn't have code, so he clicks get code.
I need that php generated code is pasted in textbox after this click...
Could someone help?
Thank you!
Button clicked, something shows in the text field. Sometimes Javascript isn't necessary.
<?php
$code = "";
if (isset($_POST['buttonId']))
{
$code = 'Some code here';
}
?>
<form method="post" >
<input name="textField" id="textField" type="text" value="<?php echo $code; ?>" />
<input name="buttonId" id="buttonId" type="submit" value="Submit" />
</form>
Assuming your html looks like this:
<button id="myPHPid">...</button>
<input type="text" id="idRecipient" />
then
<button id="myPHPid" onclick="document.getElementById('idRecipient').value = this.id">...</button>
try this code
$('button').live('click',function() {
var $butVal= $(this).val();
alert($butVal);
$('input#buttonValue').val($butVal);
return false;
});
live DEMO