I'm Jad and I'm trying to make a math rounder.
The problem is that when I click Do Math!, the echo doesn't work.
Here's my code:
<?php
if(isset($_GET['math']))
switch ($_GET['math']) {
case "round":
if (isset($_POST['submit'])){
echo round($_POST['numb1']);
} else{
?>
<h1>Math Rounding!</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr>
<td>Number:</td>
<td>
<input type="text" name="numb1" placeholder="Number 1..." maxlength="44444444444444444444444444444444444444444444444444444444444444444444">
</td>
</tr>
<tr>
<th colspan=2>
<input type="submit" name="submit" value="Do Math!" style="height: 25px; width: 100px">
</th>
</tr>
</table>
</form>
</center>
<?php
break;
}
}
?>
So in each form change the value of hidden field(<input type="hidden" name="math" value="round">) acoordingly.and also need to change the forms action to a single page.
<?php
if(isset($_POST['math']))
{
switch ($_POST['math'])
{
case "round":
echo round($_POST['numb1']);
exit;
case "round1":
echo round($_POST['numb1']);
exit;
}
}
else{
?>
<h1>Math Rounding!</h1>
<form action="<?php
echo $_SERVER['PHP_SELF'];
?>" method="post">
<input type="hidden" name="math" value="round">
<table border="0">
<tr><td>Number:</td><td>
<input type="text" name="numb1" placeholder="Number 1..." maxlength="44444444444444444444444444444444444444444444444444444444444444444444">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit"
value="Do Math!" style="height: 25px; width: 100px"></th></tr></table>
</form>
</center>
<?php
}?>
You need to set method to "get" and set form action to "" instead of .There is no need for it.
Your program should work now :)
edit:
set
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
to
<form action="" method="get">
Related
Hi While login into the site iam getting the username in hidden field while filling the form after login i need to insert the username in database.But iam getting The error as
Notice: Undefined index: email
Here is my code
index.php
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th></tr>
<tr>
<td><input type="text" name="user_name" value="" required /></td>
</tr>
details.php
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$email=$_POST['email'];
$name=$_POST['user_name'];
$query=mysql_query("INSERT INTO user_details(email,user_name)values('$email','$name')");
Can anyone help me this thanks in advance
While filling this form i need to insert the hidden field value also in database
You hidden field must be inside your form . Otherwise its value not submit form action and you will get Undefined Notice
<form method="post" action="details.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
--- rest code---
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
</form>
You are getting Undefined Index notice because you are not using your hidden input inside the <form></form>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
Keep in mind either filed hidden or visible you will define it inside the <form></form> otherwise you will get the Notices.
Side Note:
Please use mysqli_* or PDO extension, because mysql_* is deprecated and not available in PHP 7.
One more issue, your submit button also out side the <form> but it will work because you are using onclick() event, but its not good practice.
Your code is open for SQL Injection, you need to prevent with SQL Injection.
One last thing, i hope you are using session_start() on top in your file for getting $_SESSION['username']; in your original code, please recheck.
Try putting that field in form tag and you done.
<?php
$username = $_SESSION['username'];
if($username){?>
<h3> Welcome <?php echo $username; ?></h3>
<?php } else {
echo "no";
}
?>
<body>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th>
</tr>
<tr>
<td>
<input type="text" name="user_name" value="" required />
</td>
</tr>
index.php
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th></tr>
<tr>
<td><input type="text" name="user_name" value="" required /></td>
</tr>
<?php
session_start();
// you have to start the session in order to work with php session.
$username = $_SESSION['username'];
?>
> <form method="post" action="details.php" id="myform"> <input
> type='hidden' value="<?php echo $username; ?>" name='email'>
>
> <input type="button" value="Logout" id="logout"
> onClick="document.location.href='login.php'" />
>
> </form>
hidden type always contain hidden value
I am trying to retrieve value on php page but it's not retrieving value.
Here is my code
retrieve
<html>
<body>
<?php
include('conn.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
$select_table = "select * from clientreg order by id limit $start,$per_page";
$variable = mysql_query($select_table);
?>
<form name="frmUser" method="post" action="">
<div style="width:100%;">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblListForm">
<tr class="listheader">
<td></td>
<td width="230" >*****</td>
</tr>
<?php
$i=1;
$j=0;
while($row = mysql_fetch_array($variable))
{
if($j%2==0)
$classname="evenRow";
else
$classname="oddRow";?>
<tr class="<?php echo $classname;?>">
<td><input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?>" ></td>
</tr>
<?php
$j++;}
?>
<tr class="listheader">
<td colspan="9"><input type="button" name="update" id="onclick" value="Update" /> <input type="button" name="delete" value="Delete" onClick="setDeleteAction();" />
<input type="button" name="assign" id="assign" value="Assign" onClick="setLeadAssignAction();" />
<?php
$sql = mysql_query("SELECT *FROM login where role=1");
while ($row = mysql_fetch_array($sql)){
?>
<tr class="listheader">
<td><input type="checkbox" name="eid[]" value="<?php echo $row["eid"]; ?>" ><?php echo $row["username"]; ?></td>
</tr>
<?php
}
?>
</td>
</tr>
</table>
</div>
</form>
<form class="form" method ="Post"action="" id="contact">
<?php
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
for($i=0;$i<$rowCount;$i++) {
$result = mysql_query("SELECT * FROM clientreg WHERE Id='" . $_POST["users"][$i] . "'");
$row[$i]= mysql_fetch_array($result);
echo "shakti";
echo $row[$i]['id'];
}
}
?>
<img src="button_cancel.png" class="img" id="cancel"/>
<div id="left" style="height:400px;width:47%;float:left;margin-left:20px;margin-top:15px;border-radius:10px;">
<label>Lead Owner: <span>*</span></label>
<br/>
<input type="text" name="leadowner[]" id="lead" placeholder="Lead Owner"value=""/><br/>
<br/>
<label>First Name: <span>*</span></label>
<br/>
<input type="text" name="fname"id="fname" placeholder="Fname"/><br/>
<br/>
<label>Last Name: <span>*</span></label>
<br/>
<input type="text" name="lname" id="lname" placeholder="Lname"/><br/>
<br/>
<label>Mobile No: <span>*</span></label>
<br/>
<input type="text" name="mobile"id="mobile" placeholder="Mobile"/><br/>
<br/>
<label>Email Id: <span>*</span></label>
<br/>
<input type="text"name="email" id="email" placeholder="Email"/><br/>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Where am I wrong in this code?
Please sort out my problem
My problem is here
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
Any help will be appreciated
I have updated my code and added submit button.
You have two forms. Only the second form has a submit.
Form 1:
<form name="frmUser" method="post" action="">
<input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?> ">
</form>
Form 2:
<form class="form" method ="Post"action="" id="contact">
<input type="submit" name="submit" value="Submit">
</form>
The if construct does not receive the $_POST["users"], because it receives only the POST of the second submitted form.
$rowCount = count($_POST["users"]);
$rowCount will always be 0.
Form 2:
...
<?php
$rowCount = 0;
if ($_POST["users"] != "") {
$rowCount = count($_POST["users"]);
} else if ($_POST["rowcount"] != "") {
$rowCount = $_POST["rowcount"];
}
?>
...
<form class="form" method ="Post"action="" id="contact">
...
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="rowcount" value="<?php echo $rowCount; ?>">
</form>
Then the variable $rowCount will contain the count of the rows after the submit of any of the 2 forms.
OK so I have two different types of forms and I want to use one form to submit them both. I am using a auto-responder form for form1 fields= first, last and email. My second form is a contact me form fields= name. email and a body field. I am trying to use both these forms at one shot. I have a form that will post fields to each form (INDEX.PHP) the problem is I am not sure how to set up form one to post fields into form1 and form2 the way I want. I want to use the fields that match from form index.php to both the other forms (form1, form2). so name and email will go to both forms and the body field will go to form2. The main form will post first and last in the name field of form2....also as you can see from form 1 and 2 each form has a some sort of redirect page I need to work around.?
I have main form:
Index.php
<html>
<head>
<title>Main Form</title>
</head>
<body>
<h2>Winner Aution Item Request Form</h2>
<p><span class="error"><FONT><font color="red">* required field.</font></span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
First Name: <input type="text" name="first_name" value="<?php echo $first_name;?>">
<FONT><font color="red"> *</font>
<br>
Last Name: <input type="text" name="last_name" value="<?php echo $last_name;?>"> <FONT><font color="red"> *</font>
<br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<FONT><font color="red"> *</font>
<br><br><i><b>Copy and paste auction item name below.</b></i><br>
Product Name: <input type="text" name="product_Name" rows="1" cols="10">
<?php echo $product_Name;?><FONT><font color="red"> *</font>
<input type="Submit" value="Submit" name="submit"></input>
</form>
</body>
</html>
the above form was found on a website and was pretty close to what I want it to do but not exactly....
Form1
<?php session_start(); ?>
<html>
<head>
<title>autoresponder</title>
</head>
<body>
<center>
<table cellspacing="10" bgcolor="#CCCCCC" style="border: 0px solid #000000;">
<tr><td>
<form action="http://newsletter.jeremyahenry.com//s.php" method=GET>
<strong><font color="#660000">Your First Name:</font></strong>
<input type="text" name="f" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#660000">Your Last name:</font></strong>
<input type="text" name="l" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#000066">Email address:</font></strong><br>
<input type="text" name="e" style="background-color : #FFFFFF" size=20 maxlength=50>
<input type="image" src="http://blog.jeremyahenry.com/mailermanager/images/go-button.gif" name="submit" value="Submit"><br>
<input type="hidden" name="r" value="4">
<input type="hidden" name="a" value="sub">
<input type="hidden" name="ref" value="none">
<br>
<font color="#003300">HTML: <input type="RADIO" name="h" value="1">Yes
<input type="RADIO" name="h" value="0" checked="checked">No<br>
</font>
</form>
</td></tr>
</table>
</center>
</body>
</html>
Form 2
<?php session_start(); ?>
<html>
<head>
<title>request form</title>
<style>p{font:10pt arial;}</style>
</head>
<body>
<form action="contact_me/process.php" method=post>
<table align=left border=0 height=300>
<tr>
<td nowrap>
<p> Your name:
<input maxlength=25 name=name size=25>
</td>
</tr>
<tr>
<td nowrap>
<p> Your email:
<input name=from size=25 maxlength=25>
</td>
</tr>
<tr>
<td colspan=2>
<center>
<p align=center>Enter your Auction item Name below:
<br>
<textarea cols=50 name=message rows=7></textarea>
<p align=center>
<input type=submit value="Send Message">
<input type=reset value=Reset name="reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
ass you can see from the code I was going to use sessions. But not to familiar with it even though I have experience with php I am still quite new to it and need help. I want to pars form data to both forms. and the product name to form two. any help on this would be greatly appreciated.. I hope this is clear enough for some one out there to help.....
Ok New code Three forms 1.main form submits information to form1 and form2.
I have set up using a session so I have: session.php
<?php
session_start();
// store session data
$_SESSION['af_first_name'] = $af_first_name;
$_SESSION['af_last_name'] = $af_last_name;
$_SESSION['af_email'] = $af_email;
$_SESSION['cf_address'] = $cf_item_name;
?>
That handles variables.
My new code for index.php
<?php
// including the session file
require_once("session_start.php")
?>
<?php
function stripZlashes($string)
{
//This function is to strip slashes for either array or a String
if (!is_array($string)) return stripslashes($string);
$nvar = array();
foreach ($string as $key => $value)
$nvar[stripslashes($key)] = stripZlashes($value);
return $nvar;
}
?>
</head>
<body>
<h2>Winner Aution Item Request Form</h2>
<p><span class="error"><FONT><font color="red">* required field.</font></span></p>
<form name="form1">
First Name: <input type="text" name="$af_first_name" id="af_first_name" value="<?php if(isset($_SESSION['af_first_name'])){echo stripslashes($_SESSION['af_first_name']); unset($_SESSION['af_first_name']); } ?>" /><br>
Last Name: <input type="text" name="$af_last_name" id="af_last_name" value="<?php if(isset($_SESSION['af_last_name'])){echo stripslashes($_SESSION['af_last_name']); unset($_SESSION['af_last_name']); } ?>" /><br>
E-Mail: <input type="text" name="$af_email" id="af_email" value="<?php if(isset($_SESSION['af_email'])){echo stripslashes($_SESSION['af_email']); unset($_SESSION['af_email']); } ?>" /><br>
</form>
<form name="form2">Copy and Paste Auction Name Below!<br>
Product Name <br><input type="text" name="$cf_item_name" id="cf_item_name" value="<?php if(isset($_SESSION['cf_item_name'])){echo stripslashes($_SESSION['cf_item_name']); unset($_SESSION['cf_item_name']); } ?>" /><br>
<input type="Submit" value="Submit" name="submit" onsubmit="form2.submit(); form3.submit();"></input>
That form takes input and sets to session. Session.php picks it up and places it in variable form. From here I used a strip for the underscores. and when it goes to form1 and form2 varables pick it up and fill in the form. Here is my new form1
<?php
function stripZlashes($string)
{
//This function is to strip slashes for either array or a String
if (!is_array($string)) return stripslashes($string);
$nvar = array();
foreach ($string as $key => $value)
$nvar[stripslashes($key)] = stripZlashes($value);
return $nvar;
}
if(!empty($_SESSION['_mf'])): //Strip all possible back slashes
stripZlashes($_SESSION['_mf']);
endif;
// including the session file
require_once("session_start.php")
?>
<center>
<table cellspacing="10" bgcolor="#CCCCCC" style="border: 0px solid #000000;"> <tr><td>
<form action="http://newsletter.jeremyahenry.com//s.php" method=GET>
<strong><font color="#660000">Your First Name:</font></strong>
<input type="text" id="$af_first_name" value="<?php echo (!empty($_SESSION['_af']['af_first_name'])?$_SESSION['_af']['af_first_name']:'') ?>" name="f" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#660000">Your Last name:</font></strong>
<input type="text" id="$af_last_name" value="<?php echo (!empty($_SESSION['_af']['af_last_name'])?$_SESSION['_af']['af_last_name']:'') ?>" name="l" style="background-color : #FFFFFF" size=11 maxlength=40><br>
<strong><font color="#000066">Email address:</font></strong><br>
<input type="text" id="$af_email" value="<?php echo (!empty($_SESSION['_af']['af_email'])?$_SESSION['_af']['af_email']:'') ?>" name="e" style="background-color : #FFFFFF" size=20 maxlength=50>
<input type="image" src="http://newsletter.jeremyahenry.com/images/go-button.gif" name="submit" value="Submit"><br>
<input type="hidden" name="r" value="4">
<input type="hidden" name="a" value="sub">
<input type="hidden" name="ref" value="none">
<br>
<font color="#003300">HTML: <input type="RADIO" name="h" value="1">Yes
<input type="RADIO" name="h" value="0" checked="checked">No<br>
</font></form>
</td></tr></table>
</center>
<?php
if(!empty($_SESSION['_mf'])):
unset($_SESSION['_mf']);
endif;
?>
form2:
<?php
function stripZlashes($string)
{
//This function is to strip slashes for either array or a String
if (!is_array($string)) return stripslashes($string);
$nvar = array();
foreach ($string as $key => $value)
$nvar[stripslashes($key)] = stripZlashes($value);
return $nvar;
}
if(!empty($_SESSION['_mf'])): //Strip all possible back slashes
stripZlashes($_SESSION['_mf']);
endif;
// including the session file
require_once("session_start.php")
?>
<form action="process.php" method=post>
<table align=left border=0 height=300>
<tr>
<td nowrap>
<p> Your name:
<input maxlength=25 name="af first name" id="af_first_name" value="<?php echo (!empty($_SESSION['_af']['af_first_name'])?$_SESSION['_af']['af_first_name']:'') ?>" size=25> />
</td</tr>
<tr>
<td nowrap>
<p> Your email:
<input name=af email id="af email" value="<?php echo (!empty($_SESSION['_af']['af_email'])?$_SESSION['_af']['af_email']:'') ?>" size=25 maxlength=25> />
</td></tr>
<tr>
<td colspan=2>
<center>
<p align=center>Enter your Auction item Name below:
<br>
<input name="cf item name" id="cf item name" value="<?php echo (!empty($_SESSION['_cf']['cf_item_name'])?$_SESSION['_cf']['cf_item_name']:'') ?>" rows="1" />
<p align=center>
<input type=submit value="Send Message">
</td></tr></table>
</form>
</center>
Ok now i have an issue with it submitting correctly...
<form name="form1">
<input type="text" value="" name="somename" onkeyup="form2.somename.value=this.value" />
</form>
<form name="form2">
<input type="text" value="" name="somename" />
</form>
check out the above code. hope this could be helpful. Or if this is not the result which you require. so you can be brief with your requirements in reply.
I don't know the usage or need of your implementation: maybe it is like a 3 step registration form?
It could be done in different ways without complicating it too much.
You could put all three forms in the same page and only showing the right one
according to th data being posted to the same page. It is the old way of doing things.
With Ajax calls, templates and javascript it could be don simpler but it depends on
your experience.
on each element you can mention as per their relevent events, such as
Ex. onkeyup="form2.elementname.value=this.value"
I've created a 2 step form with the following code:
<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/">
<? if (!$_POST['step']) { ?>
<h1>Step 1 of 2</h1><br />
<input type="hidden" name="step" value="1" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="title" id="title" value="Title*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">Continue</button>
<? } else if ($_POST['step'] == 1) {
foreach($_POST as $name => $value) {
if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<h1>Step 2 of 2</h1><br />
<input type="hidden" name="step" value="2" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="name" id="name" value="Name*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">submit</button>
<? } else if ($_POST['step'] == 2) { //do stuff
echo "Do stuff here";
} ?>
</form>
How can I add a back button on step 2? There will be several steps added not just 2 so the user needs to be able to move forward and back through the steps whilst keeping what they've filled in.
Try this
<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/">
<? if (!$_POST['step']) { ?>
<h1>Step 1 of 2</h1><br />
<input type="hidden" name="step" value="1" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="title" id="title" value="<?= $_REQUEST["title"]?>" placeholder="Title*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">Continue</button>
<? } else if ($_POST['step'] == 1) {
$field ="";
foreach($_POST as $name => $value) {
if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; $field .= $name."=".$value."&";}
} ?>
<div><a href="<?php bloginfo('url'); ?>/contact-us/?<?= $field ?>" >Back</a></div>
<h1>Step 2 of 2</h1><br />
<input type="hidden" name="step" value="2" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="name" id="name" value="Name*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">submit</button>
<? } else if ($_POST['step'] == 2) { //do stuff
echo "Do stuff here";
} ?>
</form>
First add it on top of your page
// keep the data
$_SESSION['data'] = $_POST;
$_SESSION['step'] = $_REQUEST['step'];
Also replace the if statement to something like this:
if ($_REQUEST['step'] == 1) {
// continue
Write dynamic values on your form
<input type="text" name="title" id="title"
value="<?php echo $_SESSION['data']['title']; ?>" />
Use a link for Back and Next:
Back
Next
I guess it would works for you! :)
To navigate back and forwards you will want to pass the data from the previous form back to it again so you would need to store the value of each form somewhere. Your best bet would be to create a session and store each form info in a session variable. Then when you load each form you should check to see if the data is already stored in a session.
Then you could add a jquery button to change the value of the step and submit the form.
To do this you would need to the 'step' hidden input an id like 'step'
<input type="hidden" name="step" id="step" value="2" />
and then add a button like
<button class="back-button" type="submit" name="back" onclick="$('#step').val('1');">Back</button>
This would change the value of the step input and then submit the form taking you back to the previous form.
You would need to have another hidden input to tell the form where it came from so it would know whether to look at the session data (is you're going backwards) or the POST data (if youre going forwards).
<form name="frm1" method="post" action="/123/123/123/123/result_page.php">
<td class="search_bar"><input type="text" name="textfield" style="width:95%; height:28px; border:0px;" /></td>
<td class="search_btn"></td>
</form>
Add a submit button to your form:
<form name="frm1" method="post" action="/123/123/123/123/result_page.php">
<td class="search_bar">
<input type="text" name="searchCity" />
</td>
<td class="search_btn">
<input type="submit" value="Search" />
</td>
</form>
Having a link as you have will bypass the form and generate a GET request, not a POST.