How to not clear input fields in php form? - php

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

Related

How to bind file_get_contents() return value in text field

I want to print file_get_contents() return value in text field.
My form (input.html):
<form name="" action="form.php" method="post">
<input type="text" name="number" id="number"/>
<input type="submit" name="submit" value="go"/>
</form>
This is my view page where I want to bind (form.php)
<?php
$number = $_REQUEST['number'];
echo $number;
$data = file_get_contents('http://apis.sdsds.sds/api/Get_Loadsheet_Details/'.$number);
?>
Here is file_get_contents() return value:
[{"ID":103,"FROM_ID":1,"NAME":"CUTTACK","COMPANY_NAME":"B K TRADING","CMP_ID":8473,"LR_NO":"00107","LR_ID":752,"LMID":17,"TO_ID":4,"DESTINATION":"TALCHER","GODAWN_ID":1,"GODAWN":"BAJARKABATI ROAD","NO_OF_PKT":8.00,"TOPAY_AMOUNT":0.00,"REMARKS":"","LOADIG_STATUS":"Close","LR_STATUS":"Delivered","LOADING_SHEETNO":"00006","MANUAL_LOADSHEET_NO":"","modeof_payment":"PAID","COLLECTED_TOPAY_AMNT":0.00,"LOADFROMMST":"CUTTACK","LOADFROMMSTID":1,"DESTINATION_ID":4,"LOADDESTINATIONNAME":"TALCHER","SUFIX":"BK","MST_GODAWN":1,"GODAWNMASTER":"BAJARKABATI ROAD","LRGODAWN":"BAJARKABATI ROAD","LRSUFIX":"BK","LRGODAWNID":1,"VEHICLE_NO":"OD-05-N-3856","VEHICLEID":799,"basic_freight":320.00,"sur_charge":0.00,"hamali":16.00,"lr_charge":30.00,"service_charge":0.00,"cover_charge":0.00,"dd_charge":0.00,"dp_charge":0.00,"grand_total":366.00,"booking_incharge":"SURYA","clubpoint":0.00,"onloading_charge":0.00,"LOADSHEET_TYPE":"NORMAL","DATE":"2017-04-03T00:00:00","lrConfirmStatus":null,"lrLoadStatus":null}]
I want to bind this return value in a text field in bind.php and here is my bind code:
<input type="text" name="cmpname" value="<?php echo $data[0].COMPANY_NAME?>"/>
But it show warning message, I think some mistake in binding in above text field.
First of all you need to json_decode your text.
$data = json_decode($data);
You should be able to properly access your value with the following line
<input type="text" name="cmpname" value="<?php echo $data[0]->COMPANY_NAME?>"/>
Note that i replaced "." with "->"
In php the dot means concatenation.
Assume that you have a json file. With that way you can manipulate it,I have already the json file saved in languages folder.
<?php
if(isset($_POST['number'])){
$number = $_POST['number'];
$counter=0;
$mydata=0;
$data = file_get_contents('languages/en.json');
$d = json_decode($data);
foreach ($d as $key) {
if($counter+1==$number){
$mydata=$key;
}
$counter++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSON example</title>
</head>
<body>
<input type="text" name="cmpname" value="<?php $mydata ?>" placeholder="<?= $mydata ?>">
</body>
</html>
as the html form is
<form name="formone" action="form.php" method="POST">
<input type="text" name="number" id="number"/>
<input type="submit" name="submit" value="go"/>
</form>
In your example you move from .html to form.php and you want to put the results to third file(bind.php), you can stay at second .php file

Simple PHP: getting variable from a form input

I can't really use PHP and from what I've seen in tutorials this should work, but it doesn't:
<html>
<head></head>
<body>
<form>
<input type='text' name="name" value='myName'>
</form>
<p>
<?php
$name = $_POST['name'];
echo $name
?>
</p>
</body>
</html>
Is there a reason I can't get the value of name?
Sorry for asking such a simple question...
here is the fiddle http://jsfiddle.net/DCmu5/1/, so, please try what you said and send it to me only when it works before answering
PHP is a server-side language, so you'll need to submit the form in order to access its variables.
Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:
echo $_GET['name'];
It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:
<form method="post">
<input type='text' name="name" value="myName" />
<input type="submit" name="go" value="Submit" />
</form>
Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.
Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>
<form action="yourpagename.php" method="post">
<input type='text' name="name" value='myName'>
<button type="submit">Submit</button>
</form>
Click button and whatever is typed in your field will show up.
<html>
<body>
<form method="post" action="1.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
try this
<?php
if(isset($_REQUEST['name'])){
$name = $_REQUEST['name'];
echo $name;
}
?>

How to call php variable input text from another Php page

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'];

How to remember text input in PHP Forms?

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>

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Categories