2 different action in one form - php

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

Related

check if input has a number or just letters then echo result on submit

Im making a form that checks the Name input and echos results when the submit button is pressed using a IF statement. But i cant get it to work. What am i doing wrong?
<form id="AUTO" method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">
</div>
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>
$Name = $_POST['Name'];
if (isset($_POST['submit'])) {
//checks name field for a number if one exist echo has a number
if (preg_match('|[0-9]', $Name)) {
echo 'has a number';
} else {
echo 'Does not contain a number';
}
}
The form input tag is named name however you are trying to get value POST index of Name.
Correct would be
$Name = $_POST['name'] ;
Keep the case of names.
Forms are key are sensitive if you set the form name as lowercase
<input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">
and in server side you should catch it as lowercase like this below.
$name = $_POST['name'];
That's how you can call the variable from the form in your server side.
Hope this helps you to solve your problem.
Thanks
<form id="AUTO" method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="Name" name="Name" class="element text" size="15" maxlength="15" type="text" placeholder="Full Name" value=""></div>
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$Name = $_POST['Name'] ;
if (preg_match('#[0-9]#',$Name)){
echo 'has number';
}else{
echo 'no number';
}
?>

save fields values during form submit

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.

Get form to submit on same page? [duplicate]

This question already has an answer here:
Submission form won't stay on same page
(1 answer)
Closed 9 years ago.
Okay so, I've posted this already but still haven't found a solution. I can't seem make my form stay on the same page and I've basically tried EVERYTHING I could possibly think of.
<?php include("inc\incfiles\header.inc.php"); ?>
<?php
$reg = #$_POST['reg'];
//declaring variables to prevent errors
$fn = $ln = $un = $em = $em2 = $pswd = $pswd2 = $d = $u_check = "";
/*$fn = ""; //First Name
$ln = ""; //Last Name
$un = ""; //Username
$em = ""; //Email
$em2 = ""; //Email 2
$pawd = ""; //:Password
$pawd2 = ""; //Password 2
$d = ""; //Sign up Date
$u_check = ""; //Check if username exists*/
//registration form
$fn = mysql_real_escape_string(#$_POST['fname']);
$ln = mysql_real_escape_string(#$_POST['lname']);
$un = mysql_real_escape_string(#$_POST['username']);
$em = mysql_real_escape_string(#$_POST['email']);
$em2 = mysql_real_escape_string(#$_POST['email2']);
$pswd = mysql_real_escape_string(#$_POST['password']);
$pswd2 = mysql_real_escape_string(#$_POST['password2']);
$d = date("Y-m-d"); //Year - Month - Day
if ($reg)
{
//check all of the fields have been filled in
if ($fn && $ln && $un && $em && $em2 && $pswd && $pswd2) {
}
else{
echo "please fill in all fields...";
}
}
?>
<table class="homepageTable">
<tr>
<td width="60%" valign="top">
<center><h2>Join the community today!</h2></center>
<center><img src="images/photo.png" width="500"></center>
<form>
</td>
<td width="40%" valign="top">
<h2>Get started below...</h2>
<form action="#" method="post">
<input type="text" size="25" name="firstname" placeholder="First Name" value="<?php echo $fn; ?>"/>
<input type="text" size="25" name="lastname" placeholder="Last Name" value="<?php echo $ln; ?>"/>
<input type="text" size="25" name="username" placeholder="Username" value="<?php echo $un; ?>"/>
<input type="text" size="25" name="email" placeholder="Email" value="<?php echo $em; ?>">
<input type="text" size="25" name="email2" placeholder="Repeat Email" value="<?php echo $em2; ?>"/>
<input type="password" size="32" name="password" placeholder="Password"/>
<input type="password" size="32" name="password2" placeholder="Repeat Password"/><br />
<input type="submit" name="reg" value="Sign Up!"/>
</form>
</td>
</tr>
</table>
My biggest problem is with the:
<form action="#" method="post">
<input type="text" size="25" name="firstname" placeholder="First Name" value="<?php echo $fn; ?>"/>
<input type="text" size="25" name="lastname" placeholder="Last Name" value="<?php echo $ln; ?>"/>
<input type="text" size="25" name="username" placeholder="Username" value="<?php echo $un; ?>"/>
<input type="text" size="25" name="email" placeholder="Email" value="<?php echo $em; ?>">
<input type="text" size="25" name="email2" placeholder="Repeat Email" value="<?php echo $em2; ?>"/>
<input type="password" size="32" name="password" placeholder="Password"/>
<input type="password" size="32" name="password2" placeholder="Repeat Password"/><br />
<input type="submit" name="reg" value="Sign Up!"/>
</form>
I want to be able to press the submit button and have it stay on the same page.
I've tried leaving the blank I've tried a few other suggestions but I keep coming up with nothing. I've been trying to figure it out for 2 days now and it just won't budge. When pressing the submit button on my site in xampp, it just takes me to another page that says OBJECT NOT FOUND...etc.
If anyone can help, it would be greatly appreciated! I really don't want to have to start all over with my coding just because of one mistake.
Header.inc.php
<?php
include("inc/scripts/mysql_connect.inc.php");
?>
<html>
<head>
<link href="css\main.css" rel="stylesheet" type="text/css">
<title>website</title>
</head>
<body>
<div class="headerMenu">
<div id="wrapper">
<div class="logo">
<img src="images/Logo.png">
</div>
<div class="search_box">
<form method="GET" action="search.php" id="search">
<input name="q" type="text" size="60" placeholder="Search..."
</form>
</div>
<div id="menu">
Home
About
Sign Up
Log in
</div>
</div>
<br />
<br />
<br />
<br />
TRY USING $_SERVER['PHP_SELF']
http://php.net/manual/en/reserved.variables.server.php
By staying on the same page , I think you mean you want to submit the page , refresh & send data. if you mean that , so you may use this as your tag :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" >
However , if you want your page not to be refreshed , you need to work with Jquery , JavaScript etc.
Your problem is this line in your inc\incfiles\header.inc.php
<input name="q" type="text" size="60" placeholder="Search..."
You are missing the closing bracket -
<input name="q" type="text" size="60" placeholder="Search..." />
^^
because of that, the closing form tag on the next line is not beinging parsed, so your form in index.php is being nestled inside the <form method="GET" action="search.php" id="search">
Instead of form action="#" write form action="" Notice there are 2 ticks with nothing in between.

php fetch your details from the database via email

I want to make a text field and button that will allow the user to fetch his details on the text fields instead of writting his details every time he wants to make a new reservation.
like in this picture:
http://oi41.tinypic.com/23ie70j.jpg
I tried to make this but with my code but gives me double forms one with the details and one without.
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$q = "SELECT * FROM tabe WHERE the_email = '$email'";
$run = mysql_query($q );
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}}
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<form method="post" action="reserv page.php">
enter the email: <input type = "text" name = "email"/>
<input type = "submit" name = "submit" value="submit" />
</form>
<?php
mysql_connect("localhost","userName","password");
mysql_select_db("database_Name");
if(isset($_POST['submit']))
{
$email = $_POST['email'];
//limit the query to one entry :)
$q = "SELECT * FROM tabe WHERE the_email = '$email' LIMIT 1";
$run = mysql_query($q );
//check if email is registered
if(mysql_num_rows($run)>0)
{
//display filled up form
while($row = mysql_fetch_array($run))
{
?>
</br></br>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" value="<?php echo $row[1]; ?>" />
Last Name: <input name="lNamet" type="text" value="<?php echo $row[2]; ?>" />
User Name: <input name="uName" type="text" value="<?php echo $row[3]; ?>"/>
Email: <input name="email" type="text" value="<?php echo $row[4]; ?>" />
password: <input name="pass" type="password" value="<?php echo $row[5]; ?>"/>
contact: <input name="number" type="text" value="<?php echo $row[6]; ?>" />
<input name="confirm" type="submit" value="Confirm" />
</form>
</br></br>
<?php
}
}
//display blank form
else{
?>
<form action="payment.php" method="post" >
First Name:<input name="fName" type="text" />
Last Name: <input name="lNamet" type="text" />
User Name: <input name="uName" type="text" />
Email: <input name="email" type="text" />
password: <input name="pass" type="password" />
contact: <input name="number" type="text" />
<input name="confirm" type="submit" value="Confirm" />
</form>
<?php
}
}
?>
You're getting 2 forms because you're echoing one form if $_POST['submit'] is set and then another one regardless of anything. Print the second form only if $_POST['submit'] is not set. Since your code is so poorly written I will just give you an example:
if(isset($_POST['submit'])){
PRINT FETCHED FORM
}else{
PRINT EMPTY FORM
}
This, however, is not the "right" way to go. What people actually do is have variables null'd at start and then fill them up with data if there's a request and have a single form written in the file with input values as those variables.

How to get form fields refilled when data is incorrect

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.

Categories