Clear html text fields - php

I have an HTML form for submitting and retrieves data from MySQL database with two buttons, "Save/Submit" and "New/Reset"
It fetch data correctly from MySQL database but when I click on New/Reset button for new contact entry it couldn't clear forms text fields. My HTML and PHP codes are as under:
<?php
//Database Connection file.
include'connect.php';
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'");
While($result = mysql_fetch_assoc($sql)){
$fname = $result['fname'];
$lname = $result['lname'];
$email = $result['email'];
$contact = $result['contact'];
}
if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&&
isset($_POST['contact'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname',
'$email', '$contact')")){
echo'Contact Save Successfully.';
}else{
echo'Contact not save.';
}
}
?>
<html>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>">
//Clean all fields of forms for new entry.
<input type="reset" value="New">
//Save or submit form data into mysql database
<input type="submit" value="Save">
</form>
</html>

You can do this easily by using jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#btnReset").click(function(){
$("#fname").val("");
$("#lname").val("");
$("#email").val("");
$("#contact").val("");
});
});
</script>
</head>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>" id="fname">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>" id="lname">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>" id="email">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>" id="contact">
//Clean all fields of forms for new entry.
<input type="reset" value="New" id="btnReset">
//Save or submit form data into mysql database
<input type="submit" value="Save" id="btnSave">
</form>
</html>

Related

Cannot find the page/object after click submit the form

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.

Get the value of the textbox from 1 form to another form in php

I am trying to get the value from 1 form to another in php. In form1 there r fields such as name,email etc. After the submitting the form it goes to another form where name,email value as to display from the form1. But in my form name,email doesnt display in form2. Here is the code.
form1.php
<form method="post" name="XIForm" id="XIForm" action="registration.php">
<p><label>Name</label><br/><input type="text" name="fname" id="fname" value="" style="margin-left:30px;" placeholder="Name"></p>
<p><label>Email</label><br/><input type="text" name="email" id="email" value="" style="margin-left:30px;" placeholder="Email"></p>
<?php
include_once 'db.php';
if(isset($_POST['XISubmit'])) {
$fname= mysql_real_escape_string($_POST['fname']);
$email=$_POST['email'];
$check_email="select * from reg where email='$email'";
$run1=mysql_query($check_email);
if(mysql_num_rows($run1)>0){
echo "<script>alert('email already exits in our database. Please try with Another!')</script>";
exit(0);
}
else{
$query = "INSERT INTO reg(fname,username,password,cpassword,email) VALUES ('$fname','$username','$password', '$cpassword','$email')";
$run11=mysql_query($query);
$to=$_POST['email'];
if($run11){
$_SESSION['sess_user4']=$username;
echo "<script>alert('Registration Successful')</script>";
echo "<script>window.open('attachfile.php', '_self')</script>";
}
}
}
?>
Form2.php
<form method="post" name="XIForm" action="attach.php" enctype="multipart/form-data" onSubmit="return validate();">
<h4 style="color:#6f4617;margin-left:10px;font-size:15px;font-family:Book Antiqua;color:#168eb6"> <b>Welcome <?=$_SESSION['sess_user4']?> </b>
<br/>
<label>Confirm Name</label><br/>
<input type="text" name="name1" id="name1" style="margin-left:30px;" placeholder="Name" value="<?php if(isset($_GET['fname'])) { echo $_GET['fname']; } ?>" />
<br/><br/>
try this or more PHP Header Location with parameter
//after submitting form
header("Location: form2.php?fname=".$fname);
//then
isset($_GET['fname']){
echo $fname;
}
You are using
echo $_GET['fname']; but you are sending a post through your form?

2 different action in one form

I have a form called addcustomer.php . it's contain firsname, lastname, mobile fields.i have also 2 button on this form. onr button for saving data and the others for sending sms to customers.
i want to when i click second button 3 data fields passed to another form called smsinfo.php.
now action for saving data works good but when direct to smsinfo . there is no data on this form .
<form method ="POST" name = "custform" action="">
<div class="cell_2_2"><label class="lblfields">Firstname:</label> <input type="text" name="firstname" autocomplete="off"/></div>
<div class="cell_3_2"><label class="lblfields">Lastname :</label> <input type="text" name="lastname" autocomplete="off"/></div>
<div class="cell_5_2"><label class="lblfields">Mobile :</label> <input type="text" name="mobile" autocomplete="off"/></div>
<input type="submit" name="submit" value="Save"/>
<input type="submit" name="sendsms" value="Sms"/>
if (isset($_POST['submit']))
{
Saving code here
} else if (isset($_POST['sendsms'])) {
header("Location: smsinfo.php");
}
here code of smsinfo.php:
<?php
$Fname = $_REQUEST['firstname'];
$Lname = $_REQUEST['lastname'];
$Mob = $_REQUEST['mobile'];
?>
<html>
<body>
<form action="sendingsms.php" method="POST">
<input style="width: 100px;" name="firstname" type="text" value="<?php echo $firstname; ?>"/>
<input style="width: 100px;" name="lastname" type="text" value="<?php echo $lastname; ?>"/>
<input style="width: 100px;" name="mobile" type="text" value="<?php echo $mobile; ?>"/>
</form>
</body>
</html>
thanks all and sorry for my poor english

Pre-populate a form from a link

Apologies if this question has already been asked...
I need to the data that is entered in form1, to be sent to form2 through the URL, form2 to read the data from the URL, populate the fields of form2, submit, then redirect to a thanks page.
I was thinking of sending the information through GET in the URL.
I don't want form2 to be seen by the user, just form1 and then the thanks page if successful.
I did try this using the below method...
index.php
<?php
session_start();
?>
<form method="POST" name="test" id="test" action="process.php">
<label for="fname">First name</label>
<input type="text" name="fname" id="fname" /><br />
<label for="lname">Last name</label>
<input type="text" name="lname" id="lname" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" /><br />
<input type="submit" value="Submit" />
</form>
process.php
<?php
session_start();
//Collect data set in the URL
if (isset($_POST['fname'])) { $fname = trim($_POST['fname']); }
if (isset($_POST['lname'])) { $lname = trim($_POST['lname']); }
if (isset($_POST['email'])) { $email = trim($_POST['email']); }
// Prepare web to lead link
$url = 'success.php?fname='.$fname.'&lname='.$lname.'&email='.$email;
// GO!
$ch = curl_init($url);
curl_exec($ch);
curl_close($ch);
?>
success.php
<?php
session_start();
//Collect data set in the URL
if (isset($_GET['fname'])) { $fname = trim($_GET['fname']); }
if (isset($_GET['lname'])) { $lname = trim($_GET['lname']); }
if (isset($_GET['email'])) { $email = trim($_GET['email']); }
?>
<!DOCTYPE html>
<html>
<head>
<title>Form test</title>
</head>
<body>
<form>
<label for="fname">First name</label>
<input type="text" name="fname" id="fname" value="<?php echo isset($fname); ?>" /><br />
<label for="lname">Last name</label>
<input type="text" name="lname" id="lname" value="<?php echo isset($lname); ?>" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php echo isset($email); ?>" />
</form>
</body>
</html>
index.php being form1, process.php collecting the data and submitting the form2, success.php being form2
Any suggestions?
The form contents when submitted in index.php will be transferred to process.php using POST method and in process.php, it prepares the link and redirects the user to success.php using header() (instead of curl), and then in success.php, the form input fields are pre-populated by using <?php if(isset($fname)) echo $var; ?> (you were doing <?php echo isset($fname); ?> which will simply output 1 or 2 depending on the condition)
The changed code should look like this (tested):
process.php:
<?php
session_start();
//Collect data set in the URL
if (isset($_POST['fname'])) { $fname = trim($_POST['fname']); }
if (isset($_POST['lname'])) { $lname = trim($_POST['lname']); }
if (isset($_POST['email'])) { $email = trim($_POST['email']); }
// Prepare web to lead link
$url = 'success.php?fname='.$fname.'&lname='.$lname.'&email='.$email;
// GO!
header("Location: $url");
?>
success.php:
<?php
session_start();
//Collect data set in the URL
if (isset($_GET['fname'])) { $fname = trim($_GET['fname']); }
if (isset($_GET['lname'])) { $lname = trim($_GET['lname']); }
if (isset($_GET['email'])) { $email = trim($_GET['email']); }
?>
<!DOCTYPE html>
<html>
<head>
<title>Form test</title>
</head>
<body>
<form>
<h2> Submitted Form </h2>
<label for="fname">First name</label>
<input type="text" name="fname" id="fname" value="<?php if(isset($fname)) echo $fname; ?>" /><br />
<label for="lname">Last name</label>
<input type="text" name="lname" id="lname" value="<?php if(isset($lname)) echo $lname; ?>" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php if(isset($email)) echo $email; ?>" />
</form>
</body>
</html>
I hope this helps. Good luck!
You can send the data with POST and then save it to the SESSION, so it's available from any of your pages; the user will still be able to see the data but would require some more work by having to use the developer tools or Firebug.

Why is my form submitting too early?

I have a form on one page, and the submit button on that page leads the user to the next page of the form. I'm trying to set it up so that the info from form 1 is submitted along with form 2's info when the submit button on form 2 is clicked and all forms have been filled out, however, the first form's data is being submitted to my database when proceeding to form 2. Any ideas why this is the case? And any ideas of a solution?
Also, I am getting an error "undefined index GET" from the first form and I am struggling to understand why?
Thanks in advance!
Form 1 (parent.php)
<?php session_start();
$_SESSION['fName']=$GET['fName'];
$_SESSION['sName']=$GET['sName'];
$_SESSION['email']=$GET['email'];
$_SESSION['address']=$GET['address'];
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>table2</title>
</head>
<h1>Is this in xamppfiles and htdocs?</h1>
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value=" <?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Next">
</div>
</form>
</body>
</html>
Form 2 (child2.php)
<?php
session_start();
include("connect.php");
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>table2</title>
</head>
<body>
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_SESSION['fName'];} else { if(!isset($fName)) { echo ""; }}?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_SESSION['sName'];} else { if(!isset($sName)) { echo ""; }}?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_SESSION['email'];} else { if(!isset($email)) { echo ""; }}?>""/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_SESSION['address'];} else { if(!isset($address)) { echo ""; }}?>""/>
</br>
<input id="submit" type="submit" value="Submit"/>
</form>
<?php
}
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$fName = mysql_real_escape_string(htmlspecialchars($_POST['fName']));
$sName = mysql_real_escape_string(htmlspecialchars($_POST['sName']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
// check to make sure both fields are entered
if ($fName == '' || $sName == '' || $email == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($fName, $sName, $email, $address, $error);
}
else
{
// save the data to the database
mysql_query("INSERT formtest SET fName='$fName', sName='$sName',email='$email', address='$address'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: child2.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','');
}
?>
</body>
</html>
The action field of your parent.php is child2.php . Also, you are checking for
if (isset($_POST['submit']))
which is set true by your first page, hence, it goes inside the loop and inserts the data in your database. This can be resolved by placing this in your parent file
<input id="submit" name="submitINIT" type="submit" value="Next">
A possible solution can be, you extract the values of the first form and store it in some session variables , and finally at final submission, you can use those values for insertion.
In your child2.php, do this
if (isset($_POST['submitINIT'])){
// store all the available values in some session variables
$_SESSION['value1']=$POST['fName'];
$_SESSION['value2']=$POST['sName'];
$_SESSION['value3']=$POST['email'];
$_SESSION['value4']=$POST['address'];
}
if (isset($_POST['submit'])){
// proceed after final submission
}
Also, change the action of 2nd file to itself using this
<form action="child2.php" method="post" class="validate">
Also, do this modification in your child2.php
<input id="submit" name="submit" type="submit" value="Submit"/>

Categories