I wrote this code (which posts to a PHP page) so that if guests do not input their name, it would pop out an alert box. However, Even when I put something in the name textbox and submit it, the alert box still pop out and it would submit the $_POST['gname'] to the server.
Here’s the JavaScript:
<script type="text/javascript">
function gSubmit()
{
if (document.getElementById("gname").value.length === 0)
{
alert("For completing the form, it requires you to input a Name");
return false;
}
else
{
document.getElementById("guestbook").innerHTML=("<p><b><center>Thank you for submitting the Guestbook");
window.location = "guestbook.php";
return true;
}
}
</script>
Here’s the HTML:
<form id="guestbook" name="guestbook" method="post" action="guestbook.php">
<center>
<table border="0px">
<tr>
<td>
<p align="right">Name :</p></td><td> <input type="text" name="gname" id="gname" />
</td>
</tr>
<tr>
<td>
<p align="right">E-mail :</p></td><td><input type="text" name="gemail" />
</td>
</tr>
<tr>
<td>
<p align="right">Telephone No :</p>
</td>
<td>
<input type="text" name="gtpn" />
</td>
</tr>
<tr>
<td>
<p align="right">Product Order / Comment / Messages :</p>
</td>
<td>
<textarea name="gtxtfld" id="gtxtfld" cols="32"></textarea>
</td>
</tr>
</table>
<br /><br />
<input type="submit" value="submit" name="submit" onclick="gSubmit();" />
<input type="reset" value="reset" name="reset" />
</form>
Change your gSubmit() function to this:
if (document.getElementById("gname").value === "")
{
alert("For completing the form, it requires you to input a Name");
return false;
}
else
{
document.getElementById("guestbook").innerHTML=("<p><b><center>Thank you for submitting the Guestbook");
return true;
}
In your html, change onclick="gsubmit()" to onclick="return gsubmit()".
Note that your message in the else clause will not be displayed because the form would have submitted by then.
Also, you have to compare the value of gname field, not it's innerHTML. And it has to be compared to empty string (""), not space (" ").
To check if the textbox has a value in it, you can use something like this:
if (document.getElementById("gname").value.length > 0)
{
alert("For completing the form, it requires you to input a Name");
//by returning false, you stop the submission from happening
return false;
}
else
{
document.getElementById("guestbook").innerHTML=("<p><b><center>Thank you for submitting the Guestbook");
return true; //allow the submission to go through
}
And in you're onclick event:
onclick="return gSubmit();"
Related
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();
}
I would like click on submit and the value input in the field to be stored in database.
However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?
<tr>
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
Use Jquery ajax to do this. Try this:
HTML
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<body>
<table>
<tr>
<td><label for="Item name"><b>Finish Product:</b></label></td>
<td><input id="finish_product" type="text" maxlength="100" style="width:100px" name="finish_product" required></td>
</tr>
</table>
<input type="button" value="Save" id="submit" />
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
url: '1.php',
data: {finish_product: $('#finish_product').val()},
success: function (result) {
alert(result)
}
});
});
});
</script>
</body>
PHP
(1.php)
Note: Use mysqli_query since mysql_query is depricated in latest versions. And use bind param instead of directly appending values to query.
<?php
if (!empty($_GET)) {
$SQL = "INSERT INTO bom (finish_product) VALUES ('".$_GET['finish_product:']."')";
$result = mysql_query($SQL);
echo 1;
} else {
echo -1;
}
?>
This isn't a form, this is just a series of table elements with various inputs and a submit button. Clean up the code - there's no closing tr tag, and where is the submit button supposed to be?
Add a form element around it.
You need an method attribute to the form - in this case, "post". Without the action attribute, it will default to the same page.
<!-- add form tag -->
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<!-- added new row for submit button - you might want to have the td element span two columns? -->
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
</form>
<-- end of form -->
<?php
if(isset($_POST['submit']))
{
//see what is being POSTED - comment out this line when you're happy with the code!
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}
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.
I have a form that has a foreach loop and inside the loop I have an input type radio, so every time it has a new value like the codes below:
<?php
$aircrafts = AircraftPermit::getaircraft();
foreach($aircrafts as $aircraft)
{
$pacas = AircraftPermit::getrouteaircraft($aircraft->name ,$pilotid);
if($pacas)
{
?>
<tr>
<td>
<input type="radio" id="ac" value="" disabled="disabled"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit" disabled="disabled">
</td>
<td align="center">
<font color="green">Granted</font>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td>
<input type="radio" id="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="id" type="hidden" value="<?php echo $pilotid ;?>">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit">
</td>
<td align="center">
<font color="red">Restricted</font>
</td>
</tr>
<?php
}
}
?>
Now at the end I have a script to check if the radio button is selected like below:
<script type="text/javascript">
function radio_is_on() {
var elementId = document.getElementById('ac');
if (elementId.checked==false)
{
alert('Please select the aircraft first!');
return false;
}
if (elementId.value)
{
return true;
}
}
</script>
When the radio button is not pushed the message pops up okay but when it's pushed it returns no value and the form send null value. Please tell me where I'm wrong.
Thanks
You cannot have multiple id tags, which is probably why it's not working. Try changing it to a class instead. See below for example in jquery.
HTML:
<input type="radio" class="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
JAVASCRIPT:
function radio_is_on() {
if ($('.ac:checked').val()) {
return true;
}
else {
alert('Please select the aircraft first!');
return false;
}
}
You have same id for all radio buttons - that is not correct.
And only one button is considered all the time.
You can number them, eg. ac1, ac2 and so on.
as far as I can remember a radio button is "pushed" when it has the checked attribute, and it is not pushed when it doesn't have it.
...
if (elementId.hasOwnProperty('checked'))
...
instead of
...
if (elementId.checked==false)
...
Hi I want to validate the field values before submit the form here is my code
<table width="600" border="0" align="left">
<tr>
<script type="text/javascript">
function previewPrint()
{
var RegNumber = document.getElementById('PP_RegNoTextBox').value;
var PassportNo = document.getElementById('PP_PassportNoTextBox').value;
if (RegNumber=="")
{
alert("Please enter your Reg No.!");
document.getElementById('RegNoTextBox').focus();
return false;
}
if (PassportNo=="")
{
alert("Please enter your Passport No.!");
document.getElementById('PassportNoTextBox').focus();
return false;
}
//window.open('regformview.php?RegNumber=RegNumber');
}
</script>
<form name="ppform" onSubmit="return previewPrint();" method="post" action="regformview.php">
<td width="100" align="LEFT" class="font8"> </td>
<td width="337" align="LEFT" class="font8"><u>Preview your Application:</u> Please provide requested details.<br>
Reg No.: <input type="text" name="printregno" id="PP_RegNoTextBox" class="input2"> Passport No.: <input type="text" name="printemail" class="input2"><input type="hidden" name="flagging" id="PP_PassportNoTextBox" value="1" class="input2"> </td>
<td width="50" align="LEFT" class="font8"><div style="float:left; background-image:url(images/printPreview1.jpg); background-repeat:no-repeat;">
<input type="image" src="images/printPreview1.jpg" name="ppbutton" value="" onMouseOver="this.src='images/printPreview2.jpg'" onMouseOut="this.src='images/printPreview1.jpg'"></div></td>
</form>
</tr>
</table>
Problem is its showing the alert box correctly if it blank but the page is navigating to regformview.php after submitting. How to navigate to regformview.php only after validation.?
These are undefined:
document.getElementById('RegNoTextBox').focus();
document.getElementById('PassportNoTextBox').focus();
it should be:
document.getElementById('PP_RegNoTextBox').focus();
document.getElementById('PP_PassportNoTextBox').focus();
Everything is fine, except you have error in your script with let you submit even if validation fails.
change the following lines
document.getElementById('PassportNoTextBox').focus();
and
document.getElementById('RegNoTextBox').focus();
as
document.getElementById('PP_PassportNoTextBox').focus();
and
document.getElementById('PP_RegNoTextBox').focus();
Try this it should work.
onSubmit="previewPrint();return false;"