//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);
}
Related
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
I have a simple signup form:
<h2>Signup</h2>
<form action="actions.php">
Email:<br>
<input type="text" name="lastname">
<br>
Password:<br>
<input type="text" name="lastname">
<br>
<br>
<input type="submit" name="signupButton" value="submit">
</form>
And here is my actions.php:
<?php
echo 'test1';
if(isset($_POST['signupButton'])){
echo 'test2';
}
?>
But when I click the submit button the page that shows is only shows:
test1
Why is the button being clicked failing to pass the if statement?
you need to specify a method type (post/get)
<form action="actions.php" method="POST">
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 am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
Im trying to get a form saving my info in my database. My form below sends the code to the database but if I update the page, reload it or lust load the page the info from the form is sent to my database.
It should Work this way, when you click "Submit" then the info is sent - and only when you click submit.
I think im missing some kind of valation or something!
Here is my PHP code:
<?
include "../config.php";
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
?>
and my form looks like:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="myform" id="myform">
name: <input type="text" name="name"><br />
age: <input type="text" name="age"><br />
text: <input type="text" name="text"><br />
country: <textarea name="country" rows="5" cols="40"></textarea><br />
<input type="submit">
</form>
What im I doig wrong? Please advice.
use isset
as
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="myform" id="myform">
name: <input type="text" name="name"><br />
age: <input type="text" name="age"><br />
text: <input type="text" name="text"><br />
country: <textarea name="country" rows="5" cols="40"></textarea><br />
<input type="submit" name="submit_name">
</form>
add name attribute to submit button
<?
include "../config.php";
if(isset($_POST['submit_name']))
{
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
}
?>
Now your query run only when the submit button is clicked
You should fix your button with a name:
<input name="formSubmit" type="submit">
And then you can check if the button was actually pressed very easy
if(isset($_POST["formSubmit"]) == true) {
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
}
Try this simple code:
HTML:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="myform" id="myform">
name: <input type="text" name="name"><br />
age: <input type="text" name="age"><br />
text: <input type="text" name="text"><br />
country: <textarea name="country" rows="5" cols="40"></textarea><br />
<input type="submit" name="send">
</form>
PHP:
<?php
include "../config.php";
if (isset($_POST['send'])) {
mysql_query("insert into profiles set name= '".$_POST["name"]."', age= '".$_POST["age"]."', text= '".$_POST["text"]."', country = '".$_POST["country"]."'");
}
?>
change submit button to
<input type="submit" name="btn" value="Submit">
and
if(isset($_POST[btn]))
{
//write your query here
}
try this code when saving to database
<?php
mysql_query("INSERT INTO profiles ('name','age','text') VALUES ('".$_POST[name]."','".$_POST[age]."','".$_POST[text]."')");
fix action atribute on form without htmlspecialchars
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" name="myform" id="myform">