PHP error validation with action - php

I am trying to create a simple form that gives the users an error on the same page if they do not put in a zip code. If they put in their zip code I want the form to go to another page. Can someone please let me know what I am doing wrong?
<?php
$errors = "";
if(isset($_GET['submit'])){
if(0 === preg_match("/\S+/", $_GET['zip']))
$errors['zip'] = '<li type="square" >To proceed please enter a valid ZIP Code.</li>';
} else {
die(header("Location: success.php"));
}
?>
<?php
if ($errors != "") {
echo $errors['zip'];
}
?>
<form name="zipcodeform" id="zipcodeform" action="" method="GET">
<br />
<h3>Try your self :</h3><br />
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td align="left" valign="top">Zip Code : </td>
<td align="left" valign="top"><input type="text" name="zip" id="zip" value="<?php echo $_GET['zip']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td align="left" valign="top"><input type="submit" name="submit" id="submit" value="Get Quotes >> " /></td>
</tr>
</form>

There are a bunch of things you should look for:
Is $errors a string or an array? If it is an array, you should initialize it properly.
Use stricter error_reporting (E_ALL). You will get a notice that $_GET['plz'] isn't set when you open the page, without having submitted the form.
Always use {}. It's good coding style.
If you take all this into account the script should be running fine or at least you should see your mistakes. It runs fine at my place.

Try this,
Say your first (form.html) page is this
<html>
<head> ... </head>
<body>
<form method='get' action='proceed.php'>
Pin code : <input type='text' name='pincode'>
<input type='Submit'>
</form>
</body>
</html>
Then your PHP page that deals with the form(proceed.php);
<html>
<head> ... </head>
<body>
<?PHP
if($_GET['pincode']==''){
//Copy and paste the contents of body of 'form.html' with errors displayed!
echo'<form method="get" action="proceed.php">
Pin code : <input style="background:#FAA;" type="text" name="pincode">
<li type="square" ><font color="red">To proceed please enter a valid ZIP Code.</font></li>
<input type="Submit">';
}
</form>
</body>
</html>

You have multiple errors in your script. Try
<?php
$errors = array();
if(isset($_GET['submit'])){
if( !preg_match("/\S+/", $_GET['zip']) ){
$errors['zip'] = '<li type="square" >To proceed please enter a valid ZIP Code.</li>';
}
if( !empty($errors) ){
echo $errors['zip'];
} else {
header("Location: success.php");
exit();
}
}
?>
<form name="zipcodeform" id="zipcodeform" action="" method="GET">
<br />
<h3>Try your self :</h3><br />
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td align="left" valign="top">Zip Code : </td>
<td align="left" valign="top"><input type="text" name="zip" id="zip" value="<?php if(isset($_GET['zip'])) echo $_GET['zip']; ?>" /></td>
</tr>
<tr>
<td> </td>
<td align="left" valign="top"><input type="submit" name="submit" id="submit" value="Get Quotes >>" /></td>
</tr>
</table>
</form>

Related

Cannot POST php

I have a really simple php form to learn php. I have the code from a tutorial. My html is:
<form action="php/learnphp.php" method="post">
<table border="0">
<tr>
<td>Name</td>
<td align="center">
<input type="text" name="username" size="30" />
</td>
</tr>
<tr>
<td>Address</td>
<td align="center">
<input type="text" name="streetaddress" size="30" />
</td>
</tr>
<tr>
<td>City</td>
<td align="center">
<input type="text" name="cityaddress" size="30" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
my php file is:
<?php
$usersName = $_POST['username'];
$streetAddress = $_POST['streetaddress'];
$cityAddress = $_POST['cityaddress'];
echo '<p>Your Information</p>';
// You can combine variables with text using a .
echo $usersName. ' lives at </br>';
echo $streetAddress. ' in </br>';
echo $cityAddress. '</br></br>'; ?>
If I press the submit button i get an error that says: Cannot POST /php/learnphp.php.
i have the form in the root map and my php in a map called 'php'. I know it's probably really simple but i cant seem to figure it out.
Could be the path folder is not correct. Try make it like learnphp.php same level with <yourhtmlfile>.html. Example root/learnphp.php and root/<yourhtmlfile>.html. And you need to edit your form action
<form action="learnphp.php" method="post">
can you please try this.It's work for me.
my.php
<form action="learnphp.php" method="post">
<table border="0">
<tr>
<td>Name</td>
<td align="center">
<input type="text" name="username" size="30" />
</td>
</tr>
<tr>
<td>Address</td>
<td align="center">
<input type="text" name="streetaddress" size="30" />
</td>
</tr>
<tr>
<td>City</td>
<td align="center">
<input type="text" name="cityaddress" size="30" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" name="submit" />
</td>
</tr>
</table>
</form>
learnphp.php
<?php
if(isset($_POST["submit"]))
{
$usersName = $_POST['username'];
$streetAddress = $_POST['streetaddress'];
$cityAddress = $_POST['cityaddress'];
echo '<p>Your Information</p>';
// You can combine variables with text using a .
echo $usersName. ' lives at </br>';
echo $streetAddress. ' in </br>';
echo $cityAddress. '</br></br>';
}
?>
You can try checking if the submit button is pressed first. If not, print an error message:
<?php
if(isset($_POST["submit"]))
{
$usersName = $_POST['username'];
$streetAddress = $_POST['streetaddress'];
$cityAddress = $_POST['cityaddress'];
echo '<p>Your Information</p>';
// You can combine variables with text using a .
echo $usersName. ' lives at </br>';
echo $streetAddress. ' in </br>';
echo $cityAddress. '</br></br>';
}else {
echo "Post Submit is not clicked";
}
?>

ReCaptcha keeps saying im a bot, doesn't ever succeed?

I was using the old ReCaptcha on my site but just upgrading to the new ReCaptcha i've 3-4 different tutorials but it keeps failing and giving the fail error.
HTML
<form method="POST" action="/feedback_sent/" enctype="multipart/form-data">
<table border="0" width="100%" cellpadding="4" cellspacing="4" height="67">
<tr>
<td width="30%" height="30">Name</td>
<td width="70%" height="30">
<p><input type="text" name="name" size="41"></p>
</td>
</tr>
<tr>
<td valign="top" width="30%" height="27">Feedback</td>
<td width="70%" height="27">
<textarea rows="8" name="feedback" cols="57"></textarea></td>
</tr>
<tr>
<td valign="top" width="30%" height="27"></td>
<td width="70%" height="27">
<div class="g-recaptcha" data-sitekey="My_Key"></div>
</td>
</tr>
<tr>
<td width="100%" colspan="2">
<p align="center"><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</form>
PHP:
$secret="---my key---";
$response=$_POST["g-recaptcha-response"];
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<p>You are a bot! Go away!</p>";
}
else if ($captcha_success->success==true) {
echo "<p>You are not not a bot!</p>";
}
Step 1:
You will need to create a public and a private key here.
Step 2:
Include this library to process the information on the server.
The code:
<?php
$public = "yourkey";
$private = "yourkey";
$lang = "en";
if(isset($_POST['submit'])){
if(array_key_exists('g-recaptcha-response', $_POST)){
require 'path/to/recaptcha/autoload.php';
$c = new ReCaptcha\ReCaptcha($private);
$r = $c->verify(
$_POST["g-recaptcha-response"],
$_SERVER["REMOTE_ADDR"]
);
if($r->isSuccess()){
echo 'success';
} else {
echo 'failed';
}
} else {
die('client failure, enable js or update browser');
}
} else {
die('form failure');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>ReCaptcha 2.0</title>
</head>
<body>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<input for="email" name="email" type="email" required>
<input for="password" name="password" type="password" required>
<div class="g-recaptcha" data-sitekey="<?= $public ?>"></div>
<input type="submit" name="submit" value="Submit">
</form>
<script src='https://www.google.com/recaptcha/api.js?hl=<?= $lang ?>'></script>
</body>
</html>
This will say either success or failed depending on the input.

HTML form with results on same page with isset [duplicate]

This question already has answers here:
Checking if form has been submitted - PHP
(9 answers)
Closed 8 years ago.
I am looking to have the results of a query to be posted on the same HTML page that data is entered into. I believe that this can be done with the isset() command, but I don't truly understand what it does and what is telling my page to go to a new php url to give me my table of data.
<html>
<body>
<form action="results.php" method="post">
<table border="0">
<td align="center"><head>Orders</head>
</tr>
<tr>
<td>Enter order number:</td>
<td align="center"><input type="text" name="enter" size="3" maxlength="3"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
This is my current code. Does the
<form action="results.php" method="post">
Line tell the code to post to a new page? Does
if(isset($_POST))
go somewhere in the code in my html file or in my results.php file?
If your form is on index.php page than first thing to do is to change action="results.php" to action="index.php" so you are not redirected to result.php page. Than you can do something like this:
<html>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "Order number - " . $_POST['enter'];
}
?>
<form action="index.php" method="post">
<table border="0">
<td align="center"><head>Orders</head>
</tr>
<tr>
<td>Enter order number:</td>
<td align="center"><input type="text" name="enter" size="3" maxlength="3"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
And if you want also fill form fields with entered value than you could do this:
<html>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "Order number - " . $_POST['enter'];
}
?>
<form action="index.php" method="post">
<table border="0">
<td align="center"><head>Orders</head>
</tr>
<tr>
<td>Enter order number:</td>
<td align="center"><input type="text" name="enter" size="3" maxlength="3" value="<?php echo isset($_POST['enter'])?$_POST['enter']:'';?>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
If you want the same file to show the results,
you have to rename the .html of the current file to .php
use <form method="post" action="<?php echo $PHP_SELF;?>"> instead of <form action="results.php" method="post">
now you add if(isset($_POST)) somewhere in the same file
you can add html code as usual to a .php file but you cannot add php code to a .html file
you can use:
if(isset($_POST['enter']))
also if you want to execute the output when "enter" field doesn't have a blank value, then you can use :
if(isset($_POST['enter']) && $_POST['enter']!="")
also keep the form action value to the same page like this:
<form action="" method="post">
OR
<form action="<?php print($_SERVER['PHP_SELF']); ?>" method="post">

Submitting two form using javascript but failed

i have been trying to Submit two form using javascript and failed. i have searched stackoverflow but none of the javascript works ...
also tried this Submit two forms with one button
here is my code ... my full page is very long so i have just pasted the two form code here ...
And each field of form1 (regForm) is required .. and form2 (frmAdd) contains a while
loop... both of them forms are different i guess... so any solution with javascript or any alternative with these forms ...
<script>
submitForms = function(){
document.forms["regForm"].submit();
document.forms["frmAdd"].submit();
}
</script>
<form action="userdata.php" method="post" name="regForm" id="regForm" >
<h2><em>Personal Details : </em></h2></br>
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="22%">NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td width="78%">
<input name="full_name" type="text" id="full_name" size="40" value="<? echo $row_settings['full_name']; ?>" class="required"></td>
</tr>
<tr>
<td>FATHER'S NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="f_name" type="text" id="f_name" size="40" value="<? echo $row_settings['f_name']; ?>" class="required"></td>
</tr>
<tr>
<td>MOTHER'S NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="m_name" type="text" id="m_name" size="40" value="<? echo $row_settings['m_name']; ?>" class="required"></td>
</tr>
<tr>
<td>NATIONALITY<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="nationality" type="text" id="nationality" size="40" value="<? echo $row_settings['nationality']; ?>" class="required"></td>
</tr>
<tr>
<td>RELIGION<span class="required"><font color="#CC0000">*</font></span></td>
<td>
<input name="religion" type="text" id="religion" size="40" value="<? echo $row_settings['religion']; ?>" class="required"></td>
</tr>
</table></br>
<p align="center">
<!-- previous button <input name="doSave" type="submit" id="doSave" value="Submit"> -->
</p>
</form>
<form action="userdata.php" name="frmAdd" method="post">
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="5"> <div align="center">NO</div></td>
<td width="91"> <div align="center">Employer's NAME</div></td>
<td width="160"> <div align="center">COUNTRY</div></td>
<td width="198"> <div align="center">POSITION</div></td>
<td width="70"> <div align="center">FROM</div></td>
<td width="70"> <div align="center">TO</div></td>
<td width="70"> <div align="center">SALARY</div></td>
<td width="70"> <div align="center">REASONS FOR LEAVING</div></td>
</tr>
<?php for($i=1;$i<=4;$i++) { ?>
<tr>
<th width="5"> <div align="center"><? echo $i . "."; ?></div></th>
<td><input type="text" name="emp_name<?=$i;?>" size="25"></td>
<td><input type="text" name="emp_country<?=$i;?>" size="10"></td>
<td><input type="text" name="emp_pos<?=$i;?>" size="10"></td>
<td><input type="text" name="emp_frm<?=$i;?>" size="5"></td>
<td><input type="text" name="emp_to<?=$i;?>" size="5"></td>
<td><input type="text" name="emp_sal<?=$i;?>" size="5"></td>
<td><input type="text" name="emp_lev<?=$i;?>" size="25"></td>
</tr>
<?php } ?>
</table>
</br>
<!-- previous button <input type="submit" name="doHis" value="Save"> -->
<input type="hidden" name="hdlfrm" value="<?=$i;?>">
<input type="button" value="Click Me!" onclick="submitForms()" />
You can not submit two different forms on one page. You have a race condition.
If you want to submit two forms at once, either combine them and handle it at the server OR you can use Ajax to submit them.
I think the easiest way to do this is install jQuery and use the find method then serialize the whole thing and send it using a jQuery post, get, or ajax method.
There are missing "tags" and the code is not correct in some places.
It would be much easier if you just shortened the code to relevant parts and then posted.
So I had to do that for you.
You should test the following code. As long as that does not work, there is no need even more "form input" to write.
the test.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=UTF-8" />
<script type="text/javascript">
submitForms = function(){
document.forms["regForm"].submit();
window.setTimeout(submitB,1000)
}
function submitB() {
document.forms["frmAdd"].submit();
}
</script>
<title>Emergency Table - Update</title>
</head>
<body>
<form action="writea.php" method="post" name="regForm" id="regForm" >
<h2><em>Personal Details : </em></h2></br>
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="22%">NAME<span class="required"><font color="#CC0000">*</font></span></td>
<td width="78%">
<input name="full_name" type="text" id="full_name" size="40" value="Romeo Delta" class="required"></td>
</tr>
</table></br>
</form>
<form action="writeb.php" name="frmAdd" method="post">
<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
<tr>
<td width="5"> <div align="center">TEST</div></td>
</tr>
</table>
</br>
<input type="hidden" name="hdlfrm" value="1000">
<input type="button" value="Click Me!" onclick="submitForms()" />
</form>
</body>
</html>
writea.php
<?php
if (isset($_POST['full_name'])) { $hdl = $_POST['full_name']; } else { $hdl = "Err."; }
$fp = fopen('dataa.txt', 'a+');
fwrite($fp, "Hello from write A : ".$hdl."\r\n");
fclose($fp);
?>
writeb.php
<?php
if (isset($_POST['hdlfrm'])) { $hdl = $_POST['hdlfrm']; } else { $hdl = "Err."; }
$fp = fopen('datab.txt', 'a+');
fwrite($fp, "Hello from write B : ".$hdl."\r\n");
fclose($fp);
?>
After submit the content of dataa.txt and datab.txt should be.
dataa.txt
Hello from write A : Romeo Delta
datab.txt
Hello from write B : 1000
If you do normal form post in your page the first submit action will done and redirects to that page and second form post will not done my solution is better to use ajax post method and submit these two forms hope this helpful..
Found a Solution for this ... used this jquery and jquery form js ...
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script>
$(document).ready(function() {
$('#form1').ajaxForm(function() {
});
$('#form2').ajaxForm(function() {
alert("Your Submitted Information has been Saved !");
window.location = "userdata.php";
return true;
});
$("#submitEverything").click(function(){
mySubmitFunction();
return false;
});
});
function mySubmitFunction() {
$("#form2").submit();
$("#form1").submit();
}
</script>
userdata.php is the page where this code was pasted
and have to use two different action in forms (like form1 action="page1" and form2 action="page2")

Form not submitting html php

My form is not submitting, My database insert class works and I have tested. But my form seems not to want to submit or post values from my form. Is there something I left out?
<form name="form1" method="post" action="sub_newsletter.php" enctype="multipart/form-data">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Newsletter</td>
</tr>
<tr>
<td>Name : </td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>Email : </td>
<td><input name="email" type="text"></td>
</tr>
<tr>
<td> <input type="text" name="check_spam" id="check_spam" size="30" style="display:none;" /> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="image" name="submit" src="images/sub.jpg" style="width:180px; height:70px;"></td>
</tr>
</form>
</table>
My submit script
<?php
include('includes/database.php');
include('includes/settings.php');
include('includes/newsletter.php');
if (isset($_POST['submit'])){
//to check if posting
echo $username=rtrim($_POST['name']);
echo $myemail=rtrim($_POST['email']);
//
$check=$_POST['check_spam'];
if(!empty($check)){ echo "You are spam"; } else{
$username=rtrim($_POST['name']);
$myemail=rtrim($_POST['email']);
$news = new Newsletter();
$new->first_name=$username;
$new->email=$myemail;
$new->create();
echo "<script>alert(\"Thank you for your subscription\"); </script>";
echo "<script>window.location.replace(\"index.html\"); </script>";
}
}
?>
You obviously missed submit button
<input type="submit" name="submit">
Since you have already a field with that name, just change it or use a different name.

Categories