using $_POST in if statement - php

Suppose I have the html code below:
<FORM name="myForm" action="confirmsold.php" method="post" onsubmit="return validateFormCash()">
<table border=0 width="300" cellspacing=1 cellpadding=3 bgcolor="#353535" align="center">
<tr>
<br>
<tr>
<td bgcolor="#ffffff" colspan=2 width="30%"><b> Input Payment Details (CASH)<b></td>
</td>
</tr>
<tr>
<td bgcolor="#ffffff">Payment Type:</td>
<td bgcolor="#ffffff"><input type="text" name="paytype" value="Cash" disabled></td>
</tr>
<tr>
<td bgcolor="#ffffff">Date Sold:</td>
<td bgcolor="#ffffff"><input type="date" name="date" value="<?php echo date('Y-m-d'); ?>"></td>
</tr>
<tr>
<td bgcolor="#ffffff">Cash Amount:</td>
<td bgcolor="#ffffff"><INPUT type="TEXT" name="cashamount" size="10" maxlength="9" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"></INPUT></td>
</tr>
<td bgcolor="#ffffff" colspan=2 align="center">
<INPUT type="submit" name="submit" value="Submit">
</td>
</tr></form>
As you can see, it has a default value of "CASH". Now this will be submitted to a php page with an if statement:
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['paytype'] == 'Cash') {
This is the Javascript im using for the form:
function validateFormCash() {
var x = document.forms["myForm"]["cashamount"].value;
if (x == null || x == "") {
alert("Please input Cash Amount");
return false;
}
}
But the page will return an error saying undefined paytype, what am I missing here? Anyone please help.
Thank you

Write this in if conditional:
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['submit']) {
To be able to catch this $_POST, you have to put input field in form. For example:
<form action="" method="POST">
<!-- some HTML here, including <table> tag -->
<tr>
<td bgcolor="#ffffff">Payment Type:</td>
<td bgcolor="#ffffff"><input type="text" name="paytype" value="Cash"></td>
</tr><tr>
<td bgcolor="#ffffff" colspan=2 align="center">
<INPUT type="submit" name="submit" value="Submit">
</td>
</tr>
<!-- Some more HTML here, including </table> end tag -->
</form>

Related

Preserve form values from page to page

I have two PHP pages from one I input the year and after submitting year value goes in next page via form input as in image enter image description here
image just after clicking the submit button of first page
code for the first page is
<div class="col-sm-12" style="background-color:lavender;">
<form method="post" action="next.php">
<table id="emi" width="100%">
<tr>
<td width="100%">
<div align="center"><b>
<h3> Enter Fiancial Information:</h3>
</b> </td>
<table width="100%" border="0" id="emi">
<tr>
<td><strong>First Calender Year of DATA! ( i.e. 1999 ) </strong></td>
<td><input type="text" class="form-control" placeholder="Enter the First Assesment Year" name="year" pattern="\d*" maxlength="4"></td>
<td><input type="submit" value="Go" name="submit"class="btn btn-success"></td>
</tr></table>
</table></form>
</table>
I have two PHP pages from one I input the year and after submitting year value goes in next page via form input as in image enter image description here
In next page I have one form input also and after input the values in next page and after clicking compute button the value of year which we call from page one disappear. As shown in image
Image after clicking the compute button on next.php
code for the next.php which is called after the first page is
<?php
error_reporting(0);
$year=$_REQUEST['year'];
$x=$year+1;
$y=$x+1;
?>
<table id="emi"width="100%">
<tr>
<td width="40%"><strong>INCOME STATEMENT
</strong></td>
<td width="20%"><strong> 31-03-<?php echo $year;?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $x?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $y?></strong></td>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<table id="emi" width="100%">
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a1" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a2" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
</table>
<table id="emi" width="100%">
<tr>
<td colspan="3">
<center>
<input type="submit" value="Compute" name="submit"class="btn btn-success"></center>
</td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if($_SERVER['REQUEST_METHOD']=="POST"){
$a=$_POST['a1'];
$b=$_POST['a2'];
echo $a+$b;
}?>
I have only call the value of year then plus one in it as you see 2000 2001 2002 to next page and when I enter the value of sundry it disappears the value of year and we get only plus means value of year become 0 after clicking the compute button.
When you submit the form on the second page via your "Compute" button $_REQUEST['year'] is no longer set because it was from the previous request.
One way around this would be to add a hidden input field which stores the $_REQUEST['year'] value for the next request.
Something like this should work:
<input type="hidden" name="year" value="<? echo $_REQUEST['year']; ?>">
replace your next.php with this code
<?php
error_reporting(0);
$year=$_REQUEST['year'];
if($year==null){
$year=$_POST['year'];
}
$x=$year+1;
$y=$x+1;
?>
<table id="emi"width="100%">
<tr>
<td width="40%"><strong>INCOME STATEMENT
</strong></td>
<td width="20%"><strong> 31-03-<?php echo $year;?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $x?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $y?></strong></td>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<table id="emi" width="100%">
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a1" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a2" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
</table>
<table id="emi" width="100%">
<tr>
<td colspan="3">
<center>
<input type="hidden" id="year" name="year" value="<?php echo($year);?>">
<input type="submit" value="Compute" name="submit"class="btn btn-success"></center>
</td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if($_SERVER['REQUEST_METHOD']=="POST"){
$a=$_POST['a1'];
$b=$_POST['a2'];
echo $a+$b;
}?>

PHP function not returning values for dynamic drop down

I am facing issue in my dynamic drop down function it not return values i am unable to find where i'm doing wrong.One more issue i am facing regarding this code is that it not open drop down according to menus i.e if i select USA as a country it not show related cities of USA it always show all cities of all countries please set my code i will be highly thankful to you.
function listcatagoery()
{
$query="select * from tblcountry";
$rs=mysqli_query($query);
$html ='<select name="listcatagoery" id="listcatagoery" onchange="submitme();">';
while($row = mysqli_fetch_array($rs))
{
if($cntid == $row['cnt_id'])
{
$html.='<option selected="selected"
value="'.$row['cnt_id'].'">'.$row['cnt_name'].'</option>';
}
else
{
$html.='<option
value="'.$row['cnt_id'].'">'.$row['cnt_name'].'</option>';
}
}
$html.='</select>';
return $html;
}
///////////////////// List sub-catagoery////////
function listsubcatagoery()
{
$query = "select * from tblcity";
$rs=mysqli_query($query);
$html='<select name="listsubcatagoery" id="listsubcatagoery">';
while($row = mysqli_fetch_array($rs))
{
$html.='<option value="'.$row['cit_id'].'">
'.$row['cit_name'].'</option>';
}
$html.='</select>';
return $html;
}
<div id="main">
<div id="right">
<h2>User Registration Here...</h2><br />
<form name="frmadd" id="frmadd" action="" method="post" enctype="multipart/form-data">
<table border="1" cellpadding="5" cellspacing="4" width="90%" align="center">
<tr>
<td>Full Name</td>
<td><input type=text name="textname" size="40" id="textname" required="required"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="rdgender" value="male" size="10">Male
<input type="radio" name="rdgender" value="Female" size="10">Female</td>
</tr>
<tr>
<td>Email</td>
<td><input type=text name="textemail" size="40" id="textemail" required="required"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="textpass" size="40" id="textpass" required="required"><span class="first"></span></td>
</tr>
<tr>
<td>Country</td>
<td>
<?php echo $htmlcountry; ?>
</td>
</tr>
<tr>
<td>City</td>
<td>
<?php echo $htmlcity; ?>
</td>
</tr>
<tr>
<td>Telephone</td>
<td><input type="number" name="txttel" size="11" id="txttel" required="required"></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="txtphoto" size="40" id="txtphoto"></td>
</tr>
<tr>
<td>Signin</td>
<td colspan="2" align="center"><input type="submit" name="btnadd" id="btnadd" value="SigIn Now" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<?php echo $msg; ?>
</td>
</tr>
</table>
</form><br />
</div>

Using PHP and Forms to Manipulate Data

I am new to php and I am having a hard time finding a good tutorial on how to do what I want to do. I know I want to have the feature to be able to add three variables and put the result into a textbox on the same page. Here is the code I have. I am getting an error when trying to display the result into a textbox. Any help appreciated.
<html>
<head>
<title>Field of View Calculator</title>
</head>
<body>
<form method="post" action="Test_Calculator.php" enctype="multipart/form-data">
<table width="1000" align="center" border="10">
<tr>
<td align="center" bgcolor="khaki" colspan="6"><h1>Field of View</h1></td>
</tr>
<tr>
<td align="center" colspan="1">Input</td>
<td align="center"" colspan="1">Intermediate Variables</td>
<td align="center" colspan="1">Output</td>
</tr>
<tr>
<td align="right" colspan="1">Number of Rings:</td>
<td><input type="text" name="rings" size="40"></td>
<td><input type="text" value="<?php echo $result; ?>" /></td>
<td align="right">Number of Rings:</td>
</tr>
<tr>
<td align="right">Wavelength:</td>
<td><input type="text" name="wave" size="40"></td>
</tr>
<tr>
<td align="right">Emitter Space:</td>
<td><input type="text" name="emitter" size="40"></td>
</tr>
<tr>
<td align="right">Static Values:</td>
<td align="center" colspan="6"><textarea name="content" cols="115" rows="15"></textarea></td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</form>
</body>
</html>
<?php
$result = 1 * 764000;
?>
You have to assign a value to $result before you try to echo it out.
Move the last 5 lines to the top of the file.

How to send checkbox values in contact form using php?

Actually am having a contact form consist of check-boxes. i am able to send all the form values to email but i don't know how to send values of check-boxes selected in form.
<form id="contact_form" name="contact_form" method="post" action="contact.php">
<p id="cn">* Post Your Comments</p>
<table width="0" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><strong>Name:</strong></td>
<td><input name="contact_name" type="text" class="inp_txt" /></td>
</tr>
<tr>
<td><strong>Phone:</strong></td>
<td><input name="contact_phone" type="text" class="inp_txt" /></td>
</tr>
<tr>
<td><strong>Email:</strong></td>
<td><input name="contact_email" type="text" class="inp_txt" /></td>
</tr>
<tr>
<td><strong>Products</strong>:</td>
<td> </td>
</tr>
</table>
<table width="0" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Tomato:</td>
<td><input type="checkbox" name="check[]" value="Tomato" /></td>
<td style="padding-left:20px;">Bitter Gourd:</td>
<td style="padding-left:20px;"><input type="checkbox" name="check[]" value="Bitter Gourd" /></td>
<td style="padding-left:20px;">Capsicum:</td>
<td><input type="checkbox" name="check[]" value="Capsicum" /></td>
</tr>
<tr>
<td>Chillies:</td>
<td><input type="checkbox" name="check[]" value="Chillies" /></td>
<td style="padding-left:20px;">Ladies Finger:</td>
<td style="padding-left:20px;"><input type="checkbox" name="check[]" value="Ladies Finger" /></td>
<td style="padding-left:20px;">Cabbage:</td>
<td><input type="checkbox" name="check[]" value="Cabbage"/></td>
</tr>
<tr>
<td>Bottle Gourd:</td>
<td><input type="checkbox" name="check[]" value="Bottle Gourd" /></td>
<td style="padding-left:20px;">Drumstick:</td>
<td style="padding-left:20px;"><input type="checkbox" name="check[]" value="Drumstick"/></td>
<td style="padding-left:20px;">Beans:</td>
<td><input type="checkbox" name="check[]" value="Beans"/></td>
</tr>
</table>
<table width="0" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><strong>Location:</strong></td>
<td><input name="contact_loc" type="text" class="inp_txt" /></td>
</tr>
<tr>
<td><strong>Comments:</strong></td>
<td><textarea name="contact_text" class="txt_txt inp_txt"></textarea></td>
</tr>
</table>
<table width="0" border="0" cellspacing="0" cellpadding="0" style="margin:0px 0px 0px 77px;">
<tr>
<td><input type="submit" id="button2" name="submit" value="Submit" onclick="return validation();" class="inp_btn" /></td>
<td><input type="submit" value="Reset" class="inp_btn"/></td>
</tr>
</table>
</form>
and the php script for mail is given below
<?php
if (isset($_POST['contact_name']) && isset($_POST['contact_phone']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])){
$contact_name= $_POST['contact_name'];
$contact_phone= $_POST['contact_phone'];
$contact_email= $_POST['contact_email'];
$contact_loc= $_POST['contact_loc'];
$contact_text= $_POST['contact_text'];
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}
echo"<script> alert($check_msg); </script>;";
if (!empty($contact_name) && !empty($contact_phone) && !empty($contact_email) && !empty($contact_text)){
$to = 'rameshjoe381#gmail.com';
$subject = 'Enquiry Form Submitted.';
$body = "Name: ".$contact_name."\n\n"."Phone No: ".$contact_phone."\n\n"."Message: ".$contact_text."\n\n"."Products: ".$check_msg."\n\n"."Location: ".$contact_loc;
$headers = 'From: '.$contact_email;
if (#mail($to, $subject, $body, $headers))
{?>
<script type="text/javascript">
alert("Thanks for contacting us.. We\'ll be in touch soon.'");
</script><?
echo "<script>window.location.href='index.html' </script>";
}
else{
alert("Error Sending Mail.");
echo "<script>window.location.href='index.html' </script>";
}
}else{
alert("All Fields are required.");
echo "<script>window.location.href='index.html' </script>";
}
}
?>
can you correct the code and spot where i made mistake.
thanks for your time in advance
Change this
$check_msg .= "Checked: $value\n";
To
$check_msg .= "Checked: $value\\n";
Because otherwise in javascript it will throw error, saying string literal is not closed.
Also make the alert like following
echo "<script> alert('$check_msg'); </script>;";
It is important to know the values of a checkbox are only sent when checked. If you need them sent when they are unchecked create a hidden input with the same name right before the checkbox with the value you want sent when not checked.

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