php form image submit button - unable to submit - php

I am new to php. Though my doubt is very basic, but not understanding why my below form not able to submit? I am using actually image submit button.
Please tell me what wrong in below code:
<?php
function test(){
echo 'test';
}
var_dump($_POST['submit']); // here getting NULL, why?
if(isset($_POST['submit']))
{
test();
}
?>
<form action="." method=post name="loginform">
<table>
<tr>
<input type="password" style="display:none" />
<td width="130"><input type="password" name="password" size="15" maxlength="255" ></td>
<td width="136"><input type="image" src="button-log-in.gif" name="log in" alt=" log in" width="51" height="20" border="0"></td>
</tr>
</table>
</form>

There's no element which has 'submit' name. You're submitting two things, indicated by the name attributes: "password" and "log in". $_POST['...'] contains both of them.
If you'd like the function test() to be called upon submit, regardless of what is entered, you'd better add a hidden input field and check for its existence in PHP.
HTML:
...
<input type="hidden" name="some_name">
...
PHP:
if(isset($_POST['some_name']))
{
test();
}

Related

if(isset($_POST['submit'])) not working

I know there are cases on this, however I tried their method, but it didn't work for me.
Here is the code for isset: I tried placing this statement before <head> tag and within <body> tag but don't think it made a difference. This code resides in the same php file as the the submit button.
<?php if(isset($_POST['action'])) {
echo "testing";
exit();
}?>
Here is the form & submit button within form
<form name="createCaseForm" method = "post" action = "<?php echo url_mgt::getActionURL(); ?>" id="case-form" novalidate="novalidate">
...
<td colspan="1"><button style="float:right;" name="action" type="submit" value="create_case_submit">Submit</button></td>
...
</form>
I'm not sure if using <button> instead <input> of would make a difference. I also did if(count($_POST) > 0) and it didn't work. I'm not sure what I'm doing wrong. When I run the file, it doesn't echo "testing", but instead went straight to the next page. My apologies if it's just a minor matter, but I tried methods posted in forums, but none seems to work.
I've tried to do this:
if(isset($_POST['action']) == 0) {
echo "Testing";
} else {
echo "Testing2";
}
It displays testing when the page is being loaded, however when i click on the submit button, I was hoping to see testing2 being printed. Am i right to say that after clicking the submit button, the page would not reload twice? So there is no way to check if the submit button is being posted?
I was having the same problem as above, if(isset($_POST['submit'])) not working when submitting a form. Setting the name to action didn't work for me. The solution was to assigned the submit to the name field of my form's input element. Here I present a MCVE tested in Firefox.
Kf
index.html
<html>
<body>
<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>City</td>
<td align="center"><input type="text" name="cityaddress" size="30"/></td>
</tr>
<tr>
<td><input id="radio1" type="radio" name="selector"
value="op1" /> </td>
<td><input id="radio2" type="radio" name="selector"
value="op2" /> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
name="submit" value="SubmiT"/></td>
</tr>
</table>
</form>
</body>
</html>
learnphp.php
<html>
<head>
<title>Information Gathered</title>
</head>
<body>
<?php
echo "<p>Data processed \"NOW\"</p>";
echo "<p>Let's observe: </p>";
if(isset($_POST['submit'])){
echo "submit set<br>";
echo "Submit value=" . $_POST['submit'] . "<br>";
}
else echo "ERROR submit not set<br>";
if(isset($_POST['selector'])){
echo "Selecter set<br>";
echo "Radial selection value=" . $_POST['selector'] . "<br>";
}
else echo "ERROR Selector not set<br>";
echo "</br>";
?>
</body>
</html>
Output:
Data processed "NOW"
Let's observe:
submit set
Submit value=SubmiT
Selecter set
Radial selection value=op2
You need a form field named action
<input type="text" name="action"/>
Also, make sure that url_mgt::getActionURL() is outputting the correct URL to submit to

different form action based on two buttons

The form that I'm trying to work has two buttons:
1: for viewing the submitted information and
2: for saving the confirmed information.
Part of my form:
$sql="INSERT INTO applicant_information
(entrepreneur_name,enterprise_name,.....) values
('".$_POST['entrepreneur_name']."','".$_POST['enterprise_name']."','".$_POST['address']."'...)
<form method="post" action="business_form.php">
<table width="70%" cellspacing="2px" cellpadding="5px"style="border:1px solid black;border-collapse:collapse;">
<th colspan="8"align="left" style="border:1px solid black;"><b>Personal
Information</th>
<tr>
<td width="18" rowspan="2" style="border:1px solid black;">1</td>
<td width="142" rowspan="2"style="border:1px solid black;" >Name</td>
<td style="border:1px solid black;" colspan="2">Entrepreneur</td>
<td colspan="2"style="border:1px solid black;"><?php echo $_POST['entrepreneur_name']?>
<input id="entrepreneur_name" name="entrepreneur_name" type="hidden" value="<?php echo $_POST['entrepreneur_name']?>" />
</td>
</tr>.....
//rest of the form
<input type="submit" name="edit" style="width:10%"value="Back to edit" />
<input type="submit" name="reg"style="width:10%"value="Submit" />
What I'm trying to do is to run the query when the user hit the submit button. Any idea how to do that?
What I usually do is just have one button change the form's destination on click, then submit it. So for example:
<form action="login.php" method="POST" id="myform">
<input name="username">
<input type="password" name="password">
<input type="submit" value="Login">
<button id="js-register">Register</button>
</form>
With
$('#js-register').click(function() {
$('#myform').attr('action', 'register.php').submit();
});
Or you could have both buttons be Javascript'd and bind both of them for consistency's sake - up to you.
HTML
<form action="handle_user.php" method="POST />
<input type="submit" value="View" name="view" />
<input type="submit" value="Submit" name="submit">
</form>
Check condition in php as following way...
if($_POST["view"]) {
//User hit the view button, handle accordingly
}
if($_POST["submit"]) {
//User hit the Submit information , handle accordingly
}
You need to track the button named "reg". So right after the $sql string, you can put the following:
<?php
if (isset($_POST['reg'])) {
mysql_query($sql);
if (mysql_affected_rows() > 0) {
echo "REgistration completed";
}
else {
echo "System could not process the registration";
}
}
?>
Hope that will help you.
You could make the "edit" be a plain button, instead of a submit type. And bind a click event to it, which could either redirect to the editable form or make the form editable (which ever suits you best). Then the "reg" submit could work as it does currently to save the data.

Validating form input in PHP

is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario :
user browse to form.php
user gets an html form
user fills form and the form is sent back with POST method to form.php
form.php activate generalValidate($form) where form is the just recived form filled by user
generalValidate($form) returns true if this form is valid (for exemple properly filled), false else.
i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values?
dont eat me if its a bad question, im really just trying to learn :)
a code exemple to fill for your convinience :
<?php
function generalValidate($form) {
...
}
if (!isset($_SESSION))
session_start();
else if (generalValidate(...)) {
}
?>
<html>
<head>
</head>
<div>
<p>Register</p>
</div>
<form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
<table align="center">
<tr>
<td>First Name :</td>
<td><input name="fname" value="<?php if (isset($_POST["fname"])) {echo v($_POST["fname"]);}?>" required></input></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo v($_POST["lname"]);}?>" required></input></td>
</tr>
<tr>
<td>Email :</td>
<td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo v($_POST["email"]);} else {echo "xo#xo.xo";}?>" required></input></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
<tr>
<td>Retype password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
</table>
<input type="submit" value="Register" ></input>
</form>
</body>
</html>
Yes. Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way:
In your PHP script you could have something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Determines if the form was sent through the POST method
foreach ($_POST as $fieldName => $formValue) {
// Validate $formValue
}
}
What I think you want to ask is if form data can be manipulated without knowing the form's variable names. I say this because you want to have a general purpose function where the code can be reused for any form. Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form.
This code captures the form's input. All you have to do now is create a function that does whatever to the $name and $item values as they are looped through.
<?php
foreach($_POST as $name => $item) {
print "name::$name item::$item<br>";
}
?>
<html><title>test</title>
<form method="post">
field one: <input type="text" name="one">
<br>
field two: <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>
Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button).
Take a look at the answer here.
This is actually a canonical question for receiving form data in PHP. There are lots of ways to do it.

PHP form post- submitted message

I am using a PHP script to submit an email to the database,
after the user submit, I am doing a small validation and submit it.
everything is working just fine, but instead of postback the user to the same page with a blank textbox, I want to add a label says "Submitted successfully".
I managed to do so, but the problem is when I just refresh the page- without really pressing the "submit" button, I still get to see the message- submitted successfully...
this is a small part of my code:
<form action="<?php echo $editFormAction;?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Email:</td>
<td><span id="sprytextfield1">
<input type="text" name="Email" id="Email" value="" size="32">
<input type="submit" value="Submit"><br/>
<div id="confirm">
<?php
if(isset($_POST['Email']))
echo "<font color='green' size='5'><b>Submited Successfuly!</b></font><br/>";
?>
</div>
<span class="textfieldRequiredMsg"><font size="+2"><b>Insert an Email Address</b></font></span>
<span class="textfieldInvalidFormatMsg"><font size="+2"><b>Invalid Email Address!</b></font></span>
</span>
</td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
There are 2 ways to do it.
Send your form using AJAX. A page wouldn't be reloaded upon submit.
Use sessions to store this message, then reload page using Location: header, then display message and delete it from session.
Try
if(!empty($_POST['Email'])) {
//successful submit
}
empty will check if the value is an empty string.
You need to unset your post variable after message diaplay
Submited Successfuly!";
unset($_POST['email'];
?>

i have problem in javascript email validation

i have problem in javascript email validation,
I wrote code something like,
//emp.php
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2" >
<table>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Desired Email-ID:</td>
<td><input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
</tr>
</table>
</form>
//java scrit code for checkMail()
<script language="javascript">
function checkMail() {
var email=document.getElementById('emp_email').value;
var mail = email.value;
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
if(!reg.test(mail) || mail == "")
{
alert("Your email address isn't valid!");
return false;
}
else {
alert("Email address is okay, let's send the form!");
}
}
</script>
Plz help me thanks in advance
The problem is that there is no element with id
emp_email.
Your element has name emp_email. Add an id attribute to the text box.
<input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/>
change it to
<input type="text" name="emp_email" id="emp_email" value="" size="32" onsubmit="checkMail()";/>
Edit:
To put javascript validation on submit button change
<input type="submit" value="Insert record" onclick="checkMail();"/>
to
<input type="submit" value="Insert record" onClick="return checkMail();" />
and remove the onsubmit event from the email textbox.
phoenix has the basic error right. But:
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
Don't put form validation on a submit onclick. It may be possible to submit the form without clicking the submit button (depending on the browser and number of fields and buttons in the form). Always put it on the form itself:
<form ... onsubmit="return checkMail();">
also:
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
That's a really bad idea, like most ad-hoc “e-mail validation” regexes it will reject many completely valid e-mail addresses. Your rules for what usernames and TLDs are allowed are particularly, pointlessly, restrictive.
See this infamous regex for how to actually validate e-mail addresses. Then cry, give up, and just do basic checks instead. (Is there an ‘#’ in it? is there a ‘.’ in it? are there no spaces? well fine then.)
(Even the page-long-horror-regex can't cope with non-ASCII e-email addresses via IDN, which it would be nice to support.)
Try moving the javascript function call off the submit button and onto an onblur event on the email field itself.

Categories