form variables not passing - php

It seems my code is correct, however the posted variables in the form do not echo in the update user settings page in the form. I have echoed the posted ids from the input in the database but I cannot get the variables to show. I have done this in Codeigniter fine but am trying to do it in pure php with my own framework.
$users = new Users($db);
comes from my init.php file that is called at the beginning of the file below.
when I do
<?php var_dump($user['first_name'])?>
I get Null
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">

Try $_REQUEST['first_name'] first before submitting it to array.
<?=$_REQUEST['first_name']?>
<input type="text" name="first_name" value="<?php if (isset($_POST['first_name']) )
{echo htmlentities(strip_tags($_POST['first_name']));} else { echo
$user['first_name']; }?>">
try this code

Is your form using get or post method? If it is using get method $_POST will be null. If you use $_REQUEST, it will show the result regardless of the http method used.

try it i think its work fine and solve your problam
<?php
if(isset('name of submit button ')) // example $_POST['submit'];
{
?>
<input type="text" name="first_name" value="<?php echo htmlentities(strip_tags($_POST['first_name'])) ?> " >
<?php
}
else
{
?>
<input type="text" name="first_name" value="<?php echo $user['first_name']; ?> " >
<?php
}
?>

try it
<?php
$first_name = $_POST['first_name'];
?>
<input type="text" name="first_name" value="<?php if (isset($first_name)){ echo htmlentities(strip_tags($first_name));} else { echo $users['first_name']; } ?> ">

Related

Is it possible to store variable in input box in HTML or PHP

I would like to create an input box that is only readable and have the title of the book, is it possible that the input box can store a variable?
P.S The one I want to change is the id, now I successfully disabled the input box, but the output is $id instead of the variable that I use the $_GET method.
My code is as follow
<?php
include_once 'header.php';
$id = mysqli_real_escape_string($conn, $_GET['bookid']);
$title = mysqli_real_escape_string($conn, $_GET['title']);
?>
<section class="signup-form">
<h2>Pre-order</h2>
<div class="signup-form-form">
<form action="preorder.inc.php" method="post">
<input type="text" disabled="disabled" name="id" value= $id >
<input type="text" name="uid" placeholder="Username...">
<input type="text" name="BRO" placeholder="BookRegistrationCode...">
<button type="submit" name="submit">Pre-order</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "wronginfo") {
echo "<p>Wrong information!</p>";
}
else if ($_GET["error"] == "stmtfailed") {
echo "<p>Something went wrong!</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>Success!</p>";
}
}
?>
</section>
Put <?php echo $var; ?> inside the value attribute.
<input type="text" value="<?php echo $var; ?>" />
EDIT
As per #arkascha's comment, you can use alternative php short tags:
<input type="text" value="<?= $var; ?>" />
As per the docs: https://www.php.net/manual/en/language.basic-syntax.phptags.php

Button inside echo

Here is my php codeigniter view to show Time(If it is null,it will shows ERROR).
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php if ($result->checkInTime == '0'||'NULL'){ echo 'ERROR';} else{ echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>
How can i change the ERROR into a button or link(Which is inside the echo).
The simplest way is to use if condition outside the input:
<?php
if ($result->checkInTime == 0 || $result->checkInTime == NULL) {
// here echo your button or link
} else { ?>
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>
<?php } ?>
In the sawe way that you echo ERROR, echo the HTML you want to render, within quotes (balance the quotes and escape them appropriately).
You could use something like this -
<input type="text" class="form-control timepicker" id="checkInTime" name="checkInTime" value="<?php if ($result->checkInTime == '0'||'NULL'){ echo "<button class=\"button-class\" value=\"button\"></button>" ;} else{ echo date('H:i:s',strtotime($result->checkInTime));}?>" required parsley-maxlength="6" placeholder="checkInTime" disabled/>

"Double Load" Issue with PHP isset() and $_POST data

I know I am doing something wrong by using a combination of isset(), $_POST and $_GET but I am wondering what would be the easiest and painless way to tackle my issue.
The issue arises when I submit a HTML form... It reloads the page with the post data. I capture the submit with a php isset() function, process the $_POST data and then run a window.location to refresh the page with some $_GET data.
For example...
//example.php
...
<form action="" method="post">
<input name="stage1name" type="text" class="textfield" size="22" value="<?php echo $ClaimRow['ClaimantName']; ?>">
<input name="VehicleEngine" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleEngine']; ?>">
<input name="VehicleFuel" type="text" class="textfield" size="20" value="<?php echo $vehiclerow['VehicleFuel']; ?>">
<input name="submitInfo" type="submit" value="<?php echo $LANG_Claims_Change_Info; ?>" />
</form>
...
<?php
if (isset($_POST['submitInfo']))
{
$stage1name= mysqli_real_escape_string($db, $_POST['stage1name']);
$VehicleEngine= mysqli_real_escape_string($db, $_POST['VehicleEngine']);
$VehicleFuel= mysqli_real_escape_string($db, $_POST['VehicleFuel']);
mysqli_query($db, "DO SOME COOL SQL HERE");
//I've done what I need to do so lets reload the page with the updated data
echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";
}
?>
Like I said before, this method works fine however the user gets the effect of a "double load" and is very epiletic fit inducing.
Any ideas how best to combat this?
Thanks
EDIT - Additional Example
I realised that this one example might not make complete sense so I put together another example which hopefully will.
...
<form action="" method="post">
<select name="addresstype[]" id="multiselectfrom" onchange='this.form.submit();' size="9" style="width:110px; text-align:center;">
<option style="<?php if ($AddType == 'Claimant') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Claimant"><?php echo $LANG_Claims_Claimant; ?></option>
<option style="<?php if ($AddType == 'Vehicle') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Vehicle"><?php echo $LANG_Claims_Vehicle; ?></option>
<option style="<?php if ($AddType == 'Repairer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Repairer"><?php echo $LANG_Claims_Repairer; ?></option>
<option style="<?php if ($AddType == 'Insurer') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Insurer"><?php echo $LANG_Claims_Insurer; ?></option>
<option style="<?php if ($AddType == 'Fleet') { echo'background-color:#9AD3F1 !important;'; } ?>" value="Fleet"><?php echo $LANG_Claims_Fleet; ?></option>
</select>
</form>
...
<?php
if (isset($_POST['addresstype']))
{
foreach ($_POST['addresstype'] as $addresstype) {
$addresstype2 = $addresstype;
}
echo "<script>window.location='claims.php?claimID=" . $claim . "&claimTab=Addresses&AddressType=" . $addresstype2 . "'</script>";
}
?>
The above example is supposed to take the result of the form and change the window.location depending on the form result. It does work however it loads the page twice in doing so.
Repalace:
echo "<script>window.location='example.php?vehicle=" . $vehicleID . "&claimTab=Personal'</script>";
With:
echo "<META HTTP-EQUIV = 'Refresh' Content = '0; URL =example.php?vehicle=" . $vehicleID . "&claimTab=Personal'>";
After much testing. It turns out CMorriesy was correct. However for future reference you will need to turn output_buffering = on in php.ini and also add <?php ob_start(); ?> to the very first line of your code.
header('Location: example.php?vehicle=' . $vehicleID . '&claimTab=Personal'); die();

how to display the result after the submit php form

how to display the result after submit the form
i want a display result after submit the form for print
example 1st im filling the form submit the result after the submit i want a screen to display same result
http://www.tizag.com/phpT/examples/formexample.php
how can i do this
please help me to fix this issue.
php form code
<?php
function renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error)
{
?>
<?php
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<input name="grn" type="text" id="grn" value="<?php echo $grn; ?>" size="50" />
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<input name="name" type="text" id="name" value="<?php echo $name; ?>" size="50" />
</p>
<p><span class="style9"><strong>Roll No :</strong></span><strong> *</strong>
<input name="rollno" type="text" id="rollno" value="<?php echo $rollno; ?>" size="50" />
</p>
<p><span class="style9"><strong>Class:</strong></span><strong> *</strong>
<input name="class" type="text" id="class" value="<?php echo $class; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees Date :</strong></span><strong> *</strong>
<input id="fullDate" name="date" type="text" value="<?php echo $date; ?>" size="50" />
</p>
<p><span class="style9"><strong>Fees :</strong></span><strong> *</strong>
<input name="fees" type="text" value="<?php echo $fees; ?>" size="50" />
</p>
<span class="style9"><strong>Reference</strong></span><strong> *</strong>
<input name="reference" type="text" value="<?php echo $reference; ?>" size="50">
<br/>
<p class="style1">* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
<?php
}
include('connect-db.php');
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$grn = mysql_real_escape_string(htmlspecialchars($_POST['grn']));
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$rollno = mysql_real_escape_string(htmlspecialchars($_POST['rollno']));
$class = mysql_real_escape_string(htmlspecialchars($_POST['class']));
$fees = mysql_real_escape_string(htmlspecialchars($_POST['fees']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$reference = mysql_real_escape_string(htmlspecialchars($_POST['reference']));
// check to make sure both fields are entered
if ($grn == '' || $name == '' || $rollno == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($grn, $name, $rollno, $class, $fees, $date, $reference, $error);
}
else
{
// save the data to the database
mysql_query("INSERT fees SET grn='$grn', name='$name', rollno='$rollno', class='$class', fees='$fees', date='$date', reference='$reference'")
or die(mysql_error());
echo "<center>KeyWord Submitted!</center>";
// once saved, redirect back to the view page
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','');
}
?>
Not quite shure what you are asking.
do you need help displaying the submited form data? or more spesific display for print?
to display it you would just need to make a html page that displays it.
like:
echo '<table><tr>';
echo '<td>';
echo '<Strong>Name:</strong><br/>';
echo $name;
echo '</td>';
echo '</tr></table>';
To display just the result and not the form, when you post to the same page you need to encapsulate the for code with an if statement.
if(isset($_POST['submit'])) {
//code for the php form
}else {
//code to display form
}
Use a new page for the "action" part in the form. On that new page, simply echo the $_POST of values you want to display. If you plan on making some sort of page so that people can check their entries, you could store these $_POST-data in sessions.
Simply call function on the bottom to disply posted values :
function showFormValues()
{
?>
<div>
<p><span class="style9"><strong>G.R.N No:</strong></span><strong> *</strong>
<?php echo $_POST['grn']; ?>
</p>
<p><span class="style9"><strong>Name:</strong></span><strong> *</strong>
<?php echo $_POST['name'];
</p>
and so on.
.
.
.
.
</div>
<?php
}
?>

How to get array parameters from URL?

I have a form with 4 required inputs. They are being passed into the url if they are missing.
If all 4 is missing it'll look like:
?required[0]=field_name&required[1]=field_email&required[2]=field_number&required[3]=field_desc
If the first one field_name is missing, the array will be pushed forward one, so field_email will be [0]. Which means that the array is always in the 0,1,2,3 order.
What I want to find out is how can I retrieve the information so I can put a red border around the input that is missing? What I need is something like:
while ($i=0,$i<=3,$i++) {
do ($requiredArray[i] == 'field_email') {
echo '<script type="text/javascript">$("#name)".css("border","1px solid #ff0000");</script>';
} while ();
endwhile;
}
You can also use array_search in php as follows :
array_search("field_name",$_GET['required']);
if the value is greater than or equal to zero then you can do the processing.
Like, you can have all the values in a array variable say $required_array and you can use foreach loop like as follows:
foreach($required_array as $val){
if(!(array_search($val,$_GET['required'])>=0)){
echo '<script type="text/javascript">$("#'.$val.')".css("border","1px solid #ff0000");</script>';
}
}
Look into form validation using Jquery it will be simpler and more extensible.
http://docs.jquery.com/Plugins/Validation
Here's an example for a form that preforms the validations on the server-side:
<?php
$first_name = $_GET['fname'];
$last_name = $_GET['lname'];
$email = $_GET['email'];
$submit = $_GET['submit'];
if(empty($first_name) && !empty($submit)){
$missing_fname = 1;
}
if(empty($last_name) && !empty($submit)){
$missing_lname = 1;
}
if(empty($email) && !empty($submit)){
$missing_email = 1;
}
?>
<table><form action="" method="get">
<tr><td>Enter First Name:</td><td><input type="text" id="fname" name="fname" value="<?php echo $first_name; ?>" ></td><td><?php if($missing_fname == 1) echo "<font color='red'>This field is Mandatory!</font>" ?></td></tr>
<tr><td>Enter Last Name:</td><td><input type="text" id="lname" name="lname" value="<?php echo $last_name; ?>" ></td><td><?php if($missing_lname == 1) echo "<font color='red'>This field is Mandatory!</font>" ?></td></tr>
<tr><td>Enter email:</td><td><input type="text" id="email" name="email" value="<?php echo $email; ?>"></td><td><?php if($missing_email == 1) echo "<font color='red'>This field is Mandatory!</font>" ?></td></tr>
<tr><td></td><td><input type="submit" value="submit" name="submit" id="submit"></td></tr>
</form>
</table>

Categories