I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help
Related
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>
//passing value through url
while($rowcontent=mysqli_fetch_array($details))
{
echo "<tr><td><a href=http://localhost/study/study2/edit.php?toedit=$rowcontent[rollnumber]>edit</a></td><tr>";
}
//receiving value from url
<html>
<form method="GET" action="edit.php">
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
<?php
$rollnumber=$_GET["toedit"];
echo $rollnumber;
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}
?>
above are two parts of code, where im trying to edit values in a DB by passing value (a roll number) through url but in editing code the value is not being received correctly or some other problem i cant figure out. i did the same for deleting a value from url but it seems to work.
You can use like below:
//receiving value from url
<html>
<form method="GET" action="edit.php">
<?php
while($rowcontent=mysqli_fetch_array($details))
{
?>
<input type="hidden" name="toedit" value="<?php echo $rowcontent[rollnumber]; ?>" />
<?php
}
?>
<input type="text" name="name">Enter Name <br>
<input type="text" name="rollnumber" required>Enter Rollnumber <br>
<input type="text" name="mark">Enter Mark <br>
<input type="text" name="dept">Enter Department <br>
<input type="submit" name="submit" value="submit"> <br>
</form>
inside edit.php
<?php
if(isset($_GET["submit"]))
{
$name=$_GET["name"];
$nrollnumber=$_GET["rollnumber"];
$mark=$_GET["mark"];
$department=$_GET["dept"];
$connect=mysqli_connect("","root","","details");
mysqli_query($connect,"UPDATE student SET name='$name' rollnumber='$nrollnumber' mark='$mark' department='$department' WHERE rollnumber='$rollnumber'");
mysqli_close($connect);
}
haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...
I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.
The script for handling the action of my form redirects to the form page if the values are not in proper format. I want to fill the textfields and textarea with the faulty data the user entered on redirect to the form page. I have written the following script which redirects the page on wrong value submission, but does not fill the fields thereafter.
script on form page:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
The html part of the form:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
The redirecting script on addscript.php to which the form values are submitted:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Please telll me where is the problem and how can I solve the issue?
Thanks in advance.
session_start() must be called at the top of your PHP script to use the $_SESSION variable.