I have some data in a session and I want to save it in text box value, using PHP. But the problem is when saving, just first token of string will be saved, like below example:
<?php session_start();?>
<html >
<head>
</head>
<body>
<form >
<?php echo $_SESSION['institute']="rebaz salih" ?>
<input type="text" <?php echo "value=".$_SESSION['institute']; ?> required />
</form>
</body>
Output will be:
rebaz salih
rebaz
Couple of things:
<?php echo $_SESSION['institute']="rebaz salih" ?>
Is this supposed to be the assignment? or just a print line? in any case, try getting rid of the echo.
Both #edCoder, and #chethan194 are on the right track... the value belongs on the outside, but you really should be using a new variable for institute. For example:
//htmlspecialchars will clear out any quotes, etc so it will work in the browser.
<?php
$value = htmlspecialchars($_SESSION['institute'], ENT_QUOTES);
?>
<form >
<input type="text" value = "<?php print $value; ?>" required />
</form>
Here is the full example:
<?php
php session_start();
//i don't know where you get the institute, so i will leave it here for now...
$_SESSION['institute']="rebaz salih";
$value = '';
if (!empty($_SESSION['institute']))
{
$value = htmlspecialchars($_SESSION['institute'], ENT_QUOTES);
}
?>
<html>
<head>
</head>
<body>
<form>
<input type="text" value = "<?php print $value; ?>" required />
</form>
</body>
You can put the value attribute outside php
<?php echo $_SESSION['institute']="rebaz salih" ?>
Wrong - <input type="text" <?php echo "value=".$_SESSION['institute']; ?> />
Right - <input type="text" value = "<?php echo $_SESSION['institute'];?>" />
</form>
<input type="text" value="<?php echo $_SESSION['institute']; ?>" required/>
Related
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
I was wondering if it was possible to take HTML user input using PHP (preferably the ID or something I can use numbers in) and to save confusion just echo it back.
So I have some example code here:
<input type="number" maxlength="3" name="test" id="1">
<input type="number" maxlength="3" name="test" id="2">
<input type="number" maxlength="3" name="test" id="2">
What I was looking for is a way where I could use their input and well.... echo it back for now.
if you already know how to submit a form you can use php on the other side like this to echo it out
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php
if (isset($_POST['name'])){
$userinput = $_POST['name'];
echo $userinput;}
?>
in your example all three inputs are named "test" so youll need a different name for each one. My example above uses the "name" of the input to capture it. If your using "GET" change my $_POST['name'] to $_GET['name']
Which method did you use for these inputs? Name them differently, then retrieve the data with:
<?php echo $_GET['test1']; ?>
<?php echo $_GET['test2']; ?>
<?php echo $_GET['test3']; ?>
If you used POST type in the input method, then switch for:
<?php echo $_POST['test1']; ?>
<?php echo $_POST['test2']; ?>
<?php echo $_post['test3']; ?>
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'm trying to make a simple web application that adds scores from a physical game such as Scrabble. Most of the code is a HTML form, asking for one input per form element. It then puts the data generated from the form and initializes the appropriate variables. The one part I can't figure out is how to add the new score to the last score. I tried to add variables, like $lastScore, but that didn't seem to work either. Does anyone have any suggestions?
<?php
//Gets data from HTML form
$addScore1 = $_REQUEST['addScore1'];
$addScore2 = $_REQUEST['addScore2'];
//Generates HTML form
echo "<!DOCTYPE html>
<html>
<head>
<title>Score Add</title>
</head>
<body>
<div id=\"displayNames\">
<p>
$player1
<form method=\"post\" action=\"\">
<label for=\"addScore1\">Enter your score:</label>
<input type=\"text\" name=\"addScore1\" id=\"addScore1\" />
<input type=\"submit\" />
</form>
</p>
<p>
$player2
<form method=\"post\" action=\"\">
<label for=\"addScore2\">Enter your score:</label>
<input type=\"text\" name=\"addScore2\" id=\"addScore2\" />
<input type=\"submit\"/>
</form>
</p>
</div>
</body>
</html>";
?>
On submit, the script is calling itself and loses all the variables. You need to store their current score, e.g. in the session, in a database, or in a hidden field in the form.
With hidden field in your form:
<form method="post" action="">
<label for="addScore1">Enter your score:</label>
<input type="text" name="addScore1" id="addScore1" />
<--! the addition is done in the next line in value-->
<input type="hidden" name="oldScore1" id="oldScore1" value="<?=($oldscore1 + $addScore1)?>" />
<input type="submit" />
</form>
Do the same with your second form --> oldScore2 etc...
At the top of your script, read the oldscore from $_REQUEST
//Gets data from HTML form
$addScore1 = $_REQUEST['addScore1'];
$addScore2 = $_REQUEST['addScore2'];
$oldScore1 = $_REQUEST['oldScore1'];
$oldScore2 = $_REQUEST['oldScore2'];
// as alternative, do the addition here:
$oldScore1 += $addScore1;
$oldScore2 += $addScore2;
Rewriting your code as a whole, try it:
I edited all those <?php=$somevalue?>because they don't seem to work with your PHP-setup, and replaced them with <?php echo $somevalue> ?>... let me know how it works...
<?php
// Get data from HTML form. $_POST is fine, because form method is set to POST.
$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
// if these are numeric values, add them up
if (is_numeric($addScore1) && is_numeric($oldScore1)) $oldScore1 += $addScore1;
if (is_numeric($addScore2) && is_numeric($oldScore2)) $oldScore2 += $addScore2;
// Generate HTML form -- in HTML, much to complicated in PHP, unless it is necessary for sth else
?>
<html>
<head>
<title>Score Add</title>
</head>
<body>
<div id="displayNames">
<p><?php echo $player1; ?> current score: <?php echo $oldScore1; ?>
<form method="post" action="">
<label for="addScore1">Enter your score:</label>
<input type="text" name="addScore1" id="addScore1" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
<input type="submit" />
</p>
<p><?php echo $player2; ?> current score: <?php echo $oldScore2; ?>
<label for="addScore2">Enter your score:</label>
<input type="text" name="addScore2" id="addScore2" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
<input type=\"submit\"/>
</form>
</p>
</div>
</body>
</html>
I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />