Not sure why I am having this issue, I am making a edit form that will allow the users to update data.
I have an HTML form that looks like this:
<form enctype="multipart/form-data" method="post" action="updatefacility.php">
<label for="fac_number">Facility Number: </label>
<input type="text" id="fac_number" name="fac_number" value="<?php if (!empty($facNum)) echo $facNum; ?>" /><br />
<label for="fac_name">Facility Name: </label>
<input type="text" id="fac_name" name="fac_name" value="<?php if (!empty($facName)) echo $facName; ?>" /><br />
<label for="fac_address">Address: </label>
<input type="text" id="fac_address" name="fac_address" value="<?php if (!empty($facAddress)) echo $facAddress; ?>" /><br />
<input type="button" value="Update" name="update">
</form>
When I click my button I should be hitting updatefacility.php which looks like this:
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['update'])) {
echo "test";
}
When I am clicking my button I am expecting my form to submit and return a blank page with the word "test" on it, however nothing happens..
Am I missing something here?
Thanks
You need to change the input type from button to submit.
Like this:
<input type="submit" value="Update" name="update">
Related
I followed a online tutorial to connect my php code with MySQL. After I click the submit button, it said it cannot find the object (page). I did not see any code in my code? Any idea for me how to debug this code?
<?php
$user = 'root';
$pass = 'xxxxxx';
$db = 'testDB';
$db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");
echo"Great work!";
$id ="";
$fname="";
$midname="";
$lname="";
function getPosts()
{
$posts =array();
$posts[0]=$_POST['Contact_ID'];
$posts[1]=$_POST['first_name'];
$posts[2]=$_POST['middle_name'];
$posts[3]=$_POST['last_name'];
return $posts;
}
//search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query="select * from Contact where contact_ID =$data[0]";
$search_Result=mysql_query($db, $search_Query);
if($search_Result)
{
if(mysql_num_rows($search_Result))
{
while($row=mysql_fetch_array($search_Result))
{
$id =$row['contact_ID'];
$fname =$row['first_name'];
$midname =$row['middle_name'];
$lname =$row['last_name_name'];
}
}
else
{
echo"No data for this Id";
}
}
else
echo"Result error";
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action="php_insert_update_delete_search.php" method="post">
<input type="number", name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text", name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text", name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text", name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>
remove php_insert_update_delete_search.php from the action of your form. The form is being redirected to php_insert_update_delete_search.php page when you click search button
<form action="" method="post">
<input type="number" name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text" name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text" name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
See You Have applied an action in your form tag . This action means that when ever a user will click on any button related to that form he or she will be redirected to that page (mentioned in action ) . So u need to make a page php_insert_update_delete_search.php and has to apply logic there for inserting the data .
If your wanting the form to go to another page to run code to process what you need
<form action="pageyouneed.php" method="post">
if you want to use the current page to parse your code:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
$_SERVER["PHP_SELF"]; // Will return current document to parse code.
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'm creating update password page in php but getting problem i.e. insted of post request i'm getting get. i'm using post method in form. why this happen
please help
here is my code
<?php
if(isset($_POST['add'])){
if(empty($_POST['add'])){
$error='Username or Password did not match';
}
else
{
$password=$_POST['pass'];
$connection = mysql_connect("localhost", "root", "") or die("Connection fail");
$db=mysql_select_db("chanshal", $connection);
$result=mysql_query("UPDATE login SET password='$password' where id=1", $connection );
echo 'Entered data succesfully';
mysql_close($connection);
}
}
$title='Change Password';
$content='
<div class="gallery-box">
<form action="" method="post">
<label style="padding-right:50px;">Password</label> <input type="password" name="pass" value="">
<br />
<br />
<label style="padding-right:50px;">New Password</label> <input type="password" name="new-pass" value="">
<br />
<br />
<input style="width:150px;" name="add" type="submit" value="Update">
</form>
</div>
';
include 'admin-template.php';
You are taking $_POST['pass'], but the new password field has the name new-pass in your form:
$password=$_POST['pass'];
<input type="password" name="new-pass" value="">
So I would say your POST request is working fine, you just update your table with the same data all the time.
I have a page where there are two radio button on clicking one it shows one div and on clicking another radio button it shows another div.
In both div i have to insert the data into the database but only fist submit button is working the second button on second div is not working.Below is my code:
<body>
<center>
<h2>Choose the type </h2>
<div style="padding:25px;width: 100px;">
<input id="id_radio1" type="radio" name="name1" value="1" />Dynamic<br/>
<input id="id_radio2" type="radio" name="name0" value="0" />Image
</div>
<div align="center" style="padding:25px;width: 300px;">
<div id="div1">
<form name="frm" method="post" action="">
Caption:<input type="text" name="caption" /><br />
Dynamic:<input type="text" name="dynamictext" /><br/>
Time <input type="text" name="time" /><br />
<input type="submit" name="submitbtn1" value="Submit" />
</form>
</div>
<div id="div2">
<form enctype="multipart/form-data" action="saveimage.php" method="post" name="changer">
Path:<input name="image" accept="image/jpeg" type="file" height="50px" width="50px"><br/>
Caption:<input type="text" name="caption" />
Time:<input type="text" name="caption" />
<input type="submit" name="submitbtn0" value="Save" />
</form>
</div>
</div>
</center>
</body>
</html>
<?php
if(isset($_POST['submitbtn1']))
{
include'conn.php';
$select_db = mysql_select_db('test',$connection);
//$type1=$_POST['name1'];
$sql="INSERT INTO tbl_content(caption,dynamictext,time)VALUES('".$_POST['caption']."', '".$_POST['dynamictext']."', '".$_POST['time']."')";
if (!mysql_query($sql,$connection))
{
die('Error: ' . mysql_error());
}
}
else if(isset($_POST['submitbtn0']))
{
include'conn.php';
$select_db = mysql_select_db('test',$connection);
if ($_FILES["image"]["error"] > 0)
{
echo "<font size = '5'><font color=\"#e31919\">Error: NO CHOSEN FILE <br />";
echo"<p><font size = '5'><font color=\"#e31919\">INSERT TO DATABASE FAILED";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],"image/" . $_FILES["image"]["name"]);
$file="image/".$_FILES["image"]["name"];
$sql="INSERT INTO tbl_content (id,path,caption,time) VALUES ('','$file','".$_POST['caption']."','".$_POST['time']."')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "Picture is saved into database";
}
}
?>
I think your saveimage.php file seeking a value for $_POST['time'] but second form has no value for that. Because your second form has Time:<input type="text" name="caption" /> and there is another input for that name too.
So change
Time:<input type="text" name="caption" />
to
Time:<input type="text" name="time" />
in your second form and you may success.
also sanitize your data before enter these data to database. I think best way is use prepared statements with mysqli.
I am having trouble submitted checkbox values and details to my database.
This is my HTML:
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount"/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><a href='check.php'><input type="submit" class="btn btn-primary" value="Submit" /></a></p>
</form>
This is the check.php:
$table = $_GET['table_name'];
$column = $_GET['account'];
$dbc = mysqli_connect('localhost', 'root', 'root', 'database') or die('Connection error!');
if ($value = 1) {
$checkbox = "INSERT INTO login_table_display(`user`, `table`, `column`, `value`) VALUES(`:user`, '$table', '$column', `$value`)";
mysqli_query($dbc, $checkbox) or die('Database error, check!');
}
header('location:index.php');
As you can see above, I used variables to get other details for that checkbox to insert into the table as well.
After I press submit if the checkbox is checked, this is what's seen in the url:
http://localhost/admin/check.php?checkAccount=on&table_name=masterChart&column_name=account
Any suggestions or help will be appreciated!
The classic way to submit data is to add the value attribute to your checkboxes element in your form. On server side you have to ckeck the value for "null".
<input type="checkbox" name="checkAccount" value="putyourvaluehere"/>
Your Html is not ok
It should be
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount"/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>
Also
if(isset($_POST['checkAccount']) {
Should Be
if( isset($_POST['checkAccount']) ) {
Checkbox value will be submitted only when it's checked. Use isset($_GET['checkAccount']) for this:
$var= isset($_GET['checkAccount']) ? 1 : 0; // Or whatever values you use in DB
Try this:
First you have to edit your html code as below;
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount" value='cool'/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>
you are not giving value to check box and using submit button inside a tag, it's not good practice.
Replace:
if(isset($_POST['checkAccount'])
to:
if(isset($_GET['checkAccount'])
// your html code shoul be like this
<form method="get" action="check.php">
<input type="checkbox" name="checkAccount"/>
<input type="hidden" name="table_name" value="masterChart" />
<input type="hidden" name="column_name" value="account" />
<p><input type="submit" class="btn btn-primary" value="Submit" /></p>
</form>
<?php
$table = $_GET['table_name'];
$column = $_GET['account'];
$value = isset($_GET['checkAccount']) ? 1 : 0;
$dbc = mysqli_connect('localhost', 'root', 'root', 'database') or die('Connection error!');
if ($value == 1) {
$checkbox = "INSERT INTO login_table_display('user', 'table', 'column', 'value') VALUES(':user', '$table', '$column', '$value')";
mysqli_query($dbc, $checkbox) or die('Database error, check!');
}
header('location:index.php');
?>