populate input field with PHP variable - php

I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.

<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.

You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.

<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>

Related

PHP variable over POST with a formular

I want to send a variable via POST. The variable should not be in the URL like in this formular:
<form action="index.php?content=contact" method="POST">
<a class="article-input-a">Name:</a><br><input name="name" type="text" maxlength="30"><br>
<a class="article-input-a">E-Mail Adresse:</a><br><input name="email" type="text" maxlength="30" placeholder="beispiel#email.de"><br>
<a class="article-input-a">Website:</a><br><input name="website" type="text" maxlength="40" placeholder="www.beispiel-website.de"><br>
<button class="button" name="submit" type="submit">Beitrag erstellen</button>
</form>
In this formular i can send the variables, but i dont see them in the URL.
And i can get the variables over $_POST['name']. Now i want to send the variable $random with the formular submite button.
I hope you can undestand me.
To see them in your URL you need to use GET not POST.
<form action="index.php?content=contact" method="GET">
<a class="article-input-a">Name:</a><br><input name="name" type="text" maxlength="30"><br>
<a class="article-input-a">E-Mail Adresse:</a><br><input name="email" type="text" maxlength="30" placeholder="beispiel#email.de"><br>
<a class="article-input-a">Website:</a><br><input name="website" type="text" maxlength="40" placeholder="www.beispiel-website.de"><br>
<button class="button" name="submit" type="submit">Beitrag erstellen</button>
To send the variable, add
<input type=hidden name='random' value="<?php echo $random; ?>">
This will make a hidden variable that will be submitted.
Then access using $_GET['random']. But it will be in your URL as ?Name=&E-Mail=&Website=
if i understand correcty you want hidden inputs
<form action="index.php?content=contact" method="POST">
<a class="article-input-a">Name:</a><br><input name="name" type="text" maxlength="30"><br>
<a class="article-input-a">E-Mail Adresse:</a><br><input name="email" type="text" maxlength="30" placeholder="beispiel#email.de"><br>
<a class="article-input-a">Website:</a><br><input name="website" type="text" maxlength="40" placeholder="www.beispiel-website.de"><br>
<input type="hidden" name="content" value="contact" />
<input type="hidden" name="verName" value="varValue" />
<button class="button" name="submit" type="submit">Beitrag erstellen</button>
</form>
sending variables as part of the url is a GET request, not a POST request
then you access like
$_POST['varName']
to send that variable, you need to create a hidden field, that hold the value of that random number
<input type=hidden value="<?php echo $random; ?>">

multiple forms on a single PHP page

Im having issues with a ''profile'' page where users will be able to change their username, email, name, password and so on but it seems to have conflicts when i have more than 1 form on the page as they all work individually?
I could be missing something obvious so if anyone could help id much appreciate it.
Base
<?php
require 'core/init.php';
include 'includes/overall/header.php';
?>
<h1><p>Hello <?php echo escape($user->data()->username); ?></a>!</p></h1>
<h4><p>You joined the MMOunition community <?php echo escape($user->data()->joined); ?></a></p></h4>
<alert_banner>
<?php
if(Session::exists('home')) {
echo '<p>', Session::flash('home'), '</p>';
}
?>
</alert_banner>
<br />
<form action="changeusername.php" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php echo escape($user->data()->username); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<br />
<form action="changepassword.php" method="post">
<div class="field">
<label for="password_current">Current password:</label>
<input type="password" name="password_current" id="password_current">
</div>
<div class="field">
<label for="password_new">New password:</label>
<input type="password" name="password_new" id="password_new">
</div>
<div class="field">
<label for="password_new_again">New password again:</label>
<input type="password" name="password_new_again" id="password_new_again">
<input type="submit" value="Change">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
</br>
<form action="changename.php" method="post">
<div class="field">
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo escape($user->data()->name); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<br />
<form action="changeemail.php" method="post">
<div class="field">
<label for="email">Change email address:</label>
<input type="text" name="email" id="email" value="<?php echo escape($user->data()->email); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<?php
include 'includes/overall/footer.php';
?>
Hello You haven't mentioned what problem you actualy facing but this seems like the problem is the unique identification of a form at server side
Please see this post
Multiple HTML Forms on One Page
Hope this will help ;)
Have each form action set to the current page, and use hidden inputs to determine what action is being taken. Using this method will prevent the user from being dropped onto a different page, and puts the action being taken into a variable that you can use or manipulate.
If you want to have separate PHP scripts for actual functions, use includes or requires.
<form action="" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php echo escape($user->data()->username); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<input type="hidden" name="action" value="changeUsername">
</div>
</form>
Then the PHP script gets the action from the POST and uses a switch to determine which functions to call.
if (isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
switch ($action) {
case "changeUsername":
changeUsername();
break;
case "changePassword":
...
As mentioned, if you want to have these functions in separate php files, you can use include or require.
This conveniently drops the user right back on the same page when they update information, so that they can make additional updates.

how to get input box values which is called by ajax?

<form action="chngschdl.php" method="post">
<fieldset>
<label class="home">Flight Name</label> <select name="select_catalog_query" ><?php echo $options1; ?></select>
<br/><br/>
<label class="home">Starting Route</label> <input class="text" type="text" name="stroute" onKeyUp="numericFilter(this);" /> <label class="home">Deperture Time</label> <input class="text" type="text" name="stdrt" /><br/>
<label class="home">Ending Route</label> <input class="text" type="text" name="enroute" onKeyUp="numericFilter(this);" /> <label class="home">Arrival Time</label> <input class="text" type="text" name="enart" /><br/>
<label class="home">Break Route Number</label> <input class="text" type="number" name="bpn" maxlength='1' onkeyup="Bpoint(this.value)" /><br/>
<label id='bp' > <?php //$i=$_SESSION['point'];$_SESSION['bp']=$broute[$i];?></label>
<button class="btn">Go</button>
</fieldset>
</form>
ajax getting data from:
<?php
session_start();
if( $_SESSION['type']!='admin')
{
header("Location: index.php");
return;
}
$point=$_GET['q'];
$_SESSION['point']=$point;
$bpoption="";
for($i=0;$i<$point;$i++)
{
echo "<label>Break Route[$i]<label> <input class=text type=text name=broute[$i] /> ";
echo "<label>Arrival Time[$i]<label> <input class=text type=text name=bart[$i] /> ";
echo "<label>Departure Time[$i]<label> <input class=text type=text name=bdrt[$i] /> ";
echo "<br/><br/>";
}
?>
i have to collect every data of Break Route, Arrival time and Departure time... how i suppose to get this data and use it in another page?? is there is other way around by not using ajax... bt it must be remembered that Break Route is not predefined...
since your form fields are named like "name[$i]" it doesn't matter how many ajax-populated fields there are.
All you have to do on the page processing the form submit is:
foreach ($_POST["name"] as $name) {
// do things
}

PHP validation resets the form fields

if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one

Auto submit data to MySQL?

I am teaching myself code, and after going over PHP & MySQL tutorials, I'm still a little unsure.
I want to create a page in which the user ticks relevant checkboxes, saves the data, and can log back in another time and the ticks are saved.
I've learned how to use data that is in MySQL, but how is data auto-submitted by the user? That's got me stumped...
You need to use a form:
http://www.tizag.com/htmlT/forms.php
<form method="post" action="/your/php/script.php">
Name: <input type="text" size="10" maxlength="40" name="name"> <br />
Password: <input type="password" size="10" maxlength="10" name="password">
<input type="submit" value="Save"/>
</form>
This isn't really a question than can be answered simply, but here's the Simple answer.
First, put your checkboxes in a form:
<form action="page2.php" method="post">
<input type="checkbox" name="cb1" value="SomeValue">SomeValue</input>
</form>
Then, in page2.php, put the data (which is in the $POST array) into the MySql database using the mysql* functions (mysql_connect, mysql_select_db, mysql_query, etc).
Here is an example.
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<form action="" method=post>
<b>Company Name</b> <input name="CompanyName" type="text" value="<?php echo $row['company'] ?>" /><br>
<b>First Name</b> <input name="firstname" type="text" value="<?php echo $row['firstname'] ?>"/><br>
<b>Last Name</b> <input name="lastname" type="text" value="<?php echo $row['lastname'] ?>" /><br>
<input type="submit" value="Submit">
</form>

Categories