PHP variable into form ($username) - php

<form method="POST">
<h1>Enquiries</h1>
<span><h2 class="required">*Required</h2></span>
<div class="input-group">
<label class="sr-only">Firstname</label>
<input class="text" type="text" name="name" value="<?php print_r($username)?>" />
</div>
<label class="sr-only">Lastname</label>
<input class="text" type="text" name="Lastname" placeholder="*" required>
<label class="sr-only">Email</label>
<input class="text" type="text" name="email" placeholder="">
<label class="sr-only">Subject</label>
<input type="text" name="password" class="text" placeholder="*">
<label for="inputPassword" class="sr-only">Comment:</label>
<textarea id="text" placeholder="Write something..." required> </textarea>
<input type="submit" action="submit.php">
I need to input my $username into the firstname input field. How can I set the automatic value to be $username When a logged in user goes to send an enquiry.

<input class="text" type="text" name="name" value="<?php echo isset($username) ? $username : '' ?>" />

You need to use echo instead of print_r. Also check if the variable exists.
<input class="text" type="text" name="name" value="<?php echo !empty($username) ? $username : ''; ?>" />

Just try this
<input class="text" type="text" name="name" value="<?= $username ?>" />
Will fix it if the variable username has been initialized prior

Related

Php not inserting data

my code is not inserting any data on my php, im using a form that will display values but my code in update is not working. please help,
here is my code in php :
if (isset($_POST['update'])) {
$landowner_id = $_POST['landowner_id'];
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barnagay'];
$areacovered = $_POST['areacovered'];
$sex = $_POST['sex'];
mysqli_query($db, "UPDATE info SET firstname='$firstname', middlename='$middlename', lastnamename='$lastname', municipality='$municipality', barangay='$barangay', areacovered='$areacovered', sex='$sex' WHERE landowner_id=$landowner_id");
$_SESSION['message'] = "Address updated!";
}
here is my html
<div class="form-wrapper">
<input type="number" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id;?>" required>
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $firstname;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="middlename" placeholder="Middle Name" class="input-field" value="<?php echo $middlename;?>">
</div>
<div class="form-wrapper">
<input type="text" name="lastname" placeholder="Last Name" class="input-field" value="<?php echo $lastname;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="municipality" placeholder="Municipality" class="input-field" value="<?php echo $municipality;?>" required>
</div>
<div class="form-wrapper">
<input type="text" name="barangay" placeholder="Barangay" class="input-field" value="<?php echo $barangay;?>" >
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="areacovered" placeholder="Area Covered" class="input-field" value="<?php echo $areacovered;?>" required>
</div>
<div class="form-wrapper">
<input type="text" id = "check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex;?>" required>
<br>
<button class="btn" type="submit" name="update" >Update</button>
</div>
I don't see any "form" tag. Are you missing to wrap your "form-wrapper" into a tag? Something like this:
<form action="" method="post">
<div class="form-wrapper">
<input type="number" id="check" name="firstname" placeholder="First Name" class="input-field" value="<?php echo $landowner_id; ?>" required>
</div>
<!-- Other inputs -->
<div class="form-wrapper">
<input type="text" id="check" name="sex" placeholder="Sex" class="input-field" value="<?php echo $sex; ?>" required>
<br>
<button class="btn" type="submit" name="update">Update</button>
</div>
</form>
Other important things to consider:
Never EVER send to the DB plain inputs coming from the outside without cleaning them! Otherwise, you will be open to SQL injection. Use prepare-statements to solve this issue.
Instead of mysqli_query I recommend you to use PDO. You can prepare statement super easy. Here you can see an example of usage: https://stackoverflow.com/a/60988740/3454593

how send in html value of php to server?

I try send value of input to the server, but in server I receive NULL of variable front side:
<form method="POST" action="validationUploadEmployee.php" style="border:1px solid #ccc">
<div class="container">
<input type="number" placeholder="Enter ID" name="idW" value = '<?php echo "$idW"; ?>' disabled="disabled" required >
<input type="text" placeholder="Enter Full Name" name="name" value = '<?php echo "$nameE"; ?>' required>
<input type="email" placeholder="Enter Email" name="email" value='<?php echo "$emailE"; ?>
<input type="text" placeholder="Enter Password" name="psw" value='<?php echo "$passwordE"; ?>' maxlength="10" size="8" required>
<input type="text" placeholder="Repeat Password" name="psw-rpt" value='<?php echo "$passwordE"; ?>' maxlength="10" size="8" required>
<input type="number" placeholder="Enter Number Phone" name="phone" value='<?php echo "$phoneE"; ?>' required>
<input type="text" placeholder="" name="branch" value='<?php echo "$myBranch"; ?>' disabled="disabled" required>
server side I receive variable like that:
$idW=$_POST['idW'];//id worker
Thank you.

Codeigniter - Why creating value for input form make the whole page messed up?

I'm trying to create registration form using Codeigniter 3. This problem comes when I try to add value in each input like this:
value="<?php echo set_value('name'); ?>"
Here's my view content_register.php:
<form class="usr-input" action="/users" method="post">
<div>
<label for="name">Nama Lengkap<span style="padding-left: 90px;">:</span></label>
<input id="name" name="name" size="40" type="text" value="<?php echo set_value('name'); ?>"/>
<br>
<label for="username">Username<span style="padding-left: 124px;">:</span></label>
<input id="username" name="username" size="40" type="text" value="<?php echo set_value('username'); ?>"/>
<br>
<label for="password">Password<span style="padding-left: 125px;">:</span></label>
<input id="password" name="password" size="40" type="password" value="<?php echo set_value('password'); ?>"/>
<br>
<label for="email">Email<span style="padding-left: 155px;">:</span></label>
<input id="email" name="email" size="40" type="text" value="<?php echo set_value('email'); ?>"/>
<br>
<label for="telepon">No. Telepon<span style="padding-left: 111px;">:</span></label>
<input id="telepon" name="telepon" size="40" type="text" value="<?php echo set_value('phone'); ?>"/>
<br>
<label for="sex">Jenis Kelamin<span style="padding-left: 96px;">:</span></label>
<select>
<option value="male">Laki-laki</option>
<option value="female">Perempuan</option>
</select>
<br>
<label for="alamat">Alamat<span style="padding-left: 146px;">:</span></label>
<textarea id="alamat" name="alamat" rows="5" cols="71" type="text" value="<?php echo set_value('name'); ?>"/></textarea>
<br>
<input type="checkbox" name="vehicle" value="Bike"> Saya telah membaca dan menerima Syarat dan Peraturan dari .</input>
</div>
</form>
And the whole page messed up. It becomes like this:
Before I add value (which the code like this:)
<form class="usr-input" action="/users" method="post">
<div>
<label for="name">Nama Lengkap<span style="padding-left: 90px;">:</span></label>
<input id="name" name="name" size="40" type="text" />
<br>
<label for="username">Username<span style="padding-left: 124px;">:</span></label>
<input id="username" name="username" size="40" type="text" />
<br>
<label for="password">Password<span style="padding-left: 125px;">:</span></label>
<input id="password" name="password" size="40" type="password" />
<br>
<label for="email">Email<span style="padding-left: 155px;">:</span></label>
<input id="email" name="email" size="40" type="text" />
<br>
<label for="telepon">No. Telepon<span style="padding-left: 111px;">:</span></label>
<input id="telepon" name="telepon" size="40" type="text" />
<br>
<label for="sex">Jenis Kelamin<span style="padding-left: 96px;">:</span></label>
<select>
<option value="male">Laki-laki</option>
<option value="female">Perempuan</option>
</select>
<br>
<label for="alamat">Alamat<span style="padding-left: 146px;">:</span></label>
<textarea id="alamat" name="alamat" rows="5" cols="71" type="text" /></textarea>
<br>
<input type="checkbox" name="vehicle" value="Bike"> Saya telah membaca dan menerima Syarat dan Peraturan dari .</input>
</div>
</form>
It works well. The page should be like this:
UPDATE
Here's the controller:
<?php
class page extends CI_Controller {
function __construct() {
parent::__construct();
// path to simple_html_dom
}
function index() {
$this->load->view('page_header');
$this->load->view('content_front');
$this->load->view('page_footer');
}
function login() {
$this->load->view('page_header');
$this->load->view('content_login');
$this->load->view('page_footer');
}
function register() {
$this->load->view('page_header');
$this->load->view('content_register');
$this->load->view('page_footer');
}
}
?>
Can you help me what's wrong with this code? Or any information that I miss?
Thank you.
Change your URL into
action="/users"
to
action="<?php echo base_url()/controller/method?"
Because action="/users" is not a valid URL

Keep form values after post <?php>

I want to keep form values after submit. I found some methods something like that :
However; when I run the html file, first : the code doesn't work, second: php codes can be seen in text areas. What should I do. Thank you for reading.
<form method="post" action="mail.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" value="<?php if (isset($_POST['Name'])){echo htmlentities($_POST['Name']); }?>" />
<label for="Subject">Subject:</label>
<input type="text" name="Subject" id="Subject" value="<?php if (isset($_POST['Subject'])){echo htmlentities($_POST['Subject']); }?>"/>
<label for="Phone">Phone:</label>
<input type="text" name="Phone" id="Phone" value="<?php if (isset($_POST['Phone'])){echo htmlentities($_POST['Phone']); }?>"/>
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" value="<?php if (isset($_POST['Email'])){echo htmlentities($_POST['Email']); }?>"/>
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message">
<?php if (isset($_POST['Message'])){echo htmlentities($_POST['Message']); }?>
</textarea>
<input type="submit" name="submit" value="Send" class="submit-button" />
</form>
It's not a php page, it's html, change the extension to .php so the browser knows it's got to read some php code.
Your POSTed values will only exist once the form has been submitted, so will initially not exist. If you want 'default' values in your form fields, you need to change the code inside your value='' elements to something like:
<?php echo (isset($_POST['Name'])) ? htmlentities($_POST['Name']) : 'Default text' ?>

How to populate text fields when radio button is clicked?

I am creating a web page where there are two divs (billing details and shipping details). When the page is loaded, the billing details are automatically displayed and the shipping details remain empty. I have included two radio buttons which allows the user to choose whether or not the shipping details are the same as the billing details. If the user selects yes from the radio buttons then the same details should be displayed in the shipping details.
Note: the details are stored in database are am using php to get the data displayed
At the moment, I have only tried using
<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>
on the first name field just to see if it is working, but i doesnt seem to work.
<div id="leftprofile">
<form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
<fieldset class="billing">
<legend>Billing Details</legend><br />
<label for="fname" class="reglabel">First Name:</label>
<input required name="fname" type="text" id="fname" value="<?php echo $fname ?>"/><br />
<label for="lname" class="reglabel">Last Name:</label>
<input required name="lname" type="text" id="lname" value="<?php echo $lname ?>"/><br />
<label for="address" class="reglabel">Address:</label>
<input required name="address" id="address" type="text" value="<?php echo $address ?>"/><br />
<label for="town" class="reglabel">Town:</label>
<input required name="town" id="town" type="text" value="<?php echo $town ?>"/><br />
<label for="postcode" class="reglabel">Post Code:</label>
<input required name="postcode" id="postcode" type="text" value="<?php echo $postcode ?>"/><br />
<label for="phone" class="reglabel">Phone:</label>
<input required name="phone" id="phone" type="text" value="<?php echo $phone ?>"/><br />
<label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
<input required name="email" type="email" id="email" value="<?php echo $email ?>"/><br />
</fieldset>
</form>
</div>
<div id="rightprofile">
<form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
<fieldset class="billing">
<legend>Shipping Details</legend><br />
<form>
Same as billing address?
<input type="radio" name="shipping" id="yes" value="yes">Yes
<input type="radio" name="shipping" id="no" value="no">No<br/>
</form>
<label for="fname" class="reglabel">First Name:</label>
<input required name="fname" type="text" id="fname" value="<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>"/><br />
<label for="lname" class="reglabel">Last Name:</label>
<input required name="lname" type="text" id="lname" /><br />
<label for="address" class="reglabel">Address:</label>
<input required name="address" id="address" type="text" /><br />
<label for="town" class="reglabel">Town:</label>
<input required name="town" id="town" type="text" /><br />
<label for="postcode" class="reglabel">Post Code:</label>
<input required name="postcode" id="postcode" type="text" /><br />
<label for="phone" class="reglabel">Phone:</label>
<input required name="phone" id="phone" type="text" /><br />
<label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
<input required name="email" type="email" id="email" /><br />
</fieldset>
</form>
</div>
I have written you some simplified example code. This site contains one form, with two input fields (billing and shipping), and one checkbox to check if the shipping information is the same as the billing information. If the checkbox is checked the code will simply ignore anything typed into 'shipping'.
This would achieve what you are asking for, at least from a PHP perspective. If you are looking more for the "copy the data in those input fields, to those input fields" in real time in the browser, then that is a task for Javascript, and not PHP.
<?php
/* Check if anything was submitted */
if(isset($_POST))
{
/* Retrieve billing information */
$billing_name = $_POST['billing_name'];
$billing_addr = $_POST['billing_addr'];
/* Check if shipping same as billing */
if(isset($_POST['same']))
{
/* Shipping is the same as billing */
$shipping_name = $billing_name;
$shipping_addr = $billing_addr;
}
/* If not, set shipping to the posted value */
else
{
$shipping_name = $_POST['shipping_name'];
$shipping_addr = $_POST['shipping_addr'];
}
$insert = mysql_query(...);
}
?>
<form method="post" action="#" />
Billing information
<label for="billing_name">Name</label>
<input type="text" id="billing_name" name="billing_name" />
<label for="billing_addr">Addr</label>
<input type="text" id="billing_addr" name="billing_addr" />
<label for="same" />Is the shipping information the same as billing information?</label>
<input type="checkbox" id="same" name="same" />
Shipping information
<label for="shipping_name">Name</label>
<input type="text" id="shipping_name" name="shipping_name" />
<label for="shipping_addr">Addr</label>
<input type="text" id="shipping_addr" name="shipping_addr" />
<input type="submit" value="Register" />
</form>

Categories