I'll try to explain this as best as I can.
I have a form that accepts multiple fields, and in the end, e-mails all the fields to a specific e-mail address.
So for example, I have three text boxes, one list box and two submit buttons.
Two of the text boxes are first name, and e-mail address
The third text box is used to populate the list box. So if I enter, NIKE, into the third text box and push the first submit button. Nike will now be in the listbox.
I want to be able to populate the list box with as many entries as needed, then push the second submit button to send all information (first name, e-mail address and all items in list box).
The problem is, pushing the first submit button always triggers the e-mail sent, since I'm "POST"ing.
I have everything working right now. The third text box submits the new data to a table in mysql, and then retrieves all the data and puts it in the list box.
What's the best way to fix this scenario? Could I stop the Post variable from validating, until the second submit button is used?
Also, I'd like to avoid Javascript, thanks
Make sure the two submit buttons have names. I.E: <input type="submit" name="command" value="Add"> and <input type="submit" name="command" value="Send">. Then you can use PHP to determine which one was clicked:
if($_REQUEST['command'] == 'Add')
{
// Code to add the item to the list box here
}
elseif($_REQUEST['command'] == 'Send')
{
// Code to send the email here...
}
BONUS: For extra credit, make the commands variables so they can be easily changed, and map them to functions...
<?php
$commands = array(
'doSendEmail' => 'Send Email',
'doAddOption' => 'Add Option',
);
function doSendEmail()
{
// your email sending code here...
}
function doAddOption()
{
// your option adding code here...
}
function printForm()
{
global $commands;
?>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="name"><br>
<input type="text" name="add">
<input type="submit" name="command" value="<?= $commands['doAddOption'] ?>">
<select>
<?php /* some code here */ ?>
</select>
<input type="submit" name="command" value="<?= $commands['doSendEmail'] ?>">
<?php
}
if(isset($_REQUEST['command']))
{
$function = array_search($_REQUEST['command'],$commands);
if($function !== -1)
call_user_func($function);
}
Related
I am looking for some help about how to make input form handling in PHP.
What I need is when a user writes data into a text form (table1), and moves to another text form (like pressing TAB, or selecting with mouse), then it should start and MySQL query to see if such data written at table1 already existing in the matching MySQL table.
My goal is like to do it without pressing submit button. Something like when google checks if an username you want to register already exists.
I am thinking about something like this:
$duplicate_data_test ="";
if (focus has moved to another form field - how to check ?) {
$query = "SELECT table1 FROM testdatabase WHERE table1 = "' . (table1 from the form below - how to get data from the this form field without POST?) .'";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0) {
$duplicate_data_test = "This data is already found in the database. Choose something else";
}
}
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">';
echo '<input type="text" maxlength="30" name="table1">';
echo '<span class="duplicaterror">'. $duplicate_data_test.' </span>';
echo '<input type="text" maxlength="30" name="table2">';
echo '<input type="submit" value="OK">';
echo '</form>';
Thank you very much for your help!
You cannot do your "interface" check with php.
Your "focus has moved to another form field" has to be done with javascript.
First, Build your form with html like this
<form name="testForm" action="postForm.php" method="POST" id="myForm">
<label>INPUT 1 : </label>
<input type="text" id="in1" value="" name="input1" />
<label>INPUT 2 : </label>
<input type="text" id="in2" value="" name="input2" />
<div style="color:red;font-weight:bold;" id="error"></div>
<button id="submitButton">SUBMIT</button>
</form>
Then make your checks when user clicks on submit button with javascript/jquery & ajax (prevent event form posting) like this :
$(document).on('click','#submitButton',function(event){
event.preventDefault();
if($.trim($('#in1').val()) == ''){
//input 1 is empty
$("#error").html('INPUT ONE IS EMPTY');
}//....continue checks
Finally, if your checks are good, then post your form
$("#myForm").submit();
and if your checks are not good then display user a message!
$("#error").html("MESSAGE!");
I made you a little example on how to do it (it's not the best way to do it but it's just an example) on jsfiddle, check this link : http://jsfiddle.net/9ayo89jt/2/
hope it helps!
checking if something exists will need an AJAX call
put the query that checks the database in a separate php file and call it with AJAX
to submit once all input fields are filled, you will need to use javascript .. check if field 1,2,3,..etc. are not empty .. formName.submit()
this is a bad approach in my opinion
I am trying to put together a form that will allow me to create a shopping cart application for a PHP class. For the purposes of this, we are supposed to display our form information repeatedly. While I'm familiar with storing information, I do not know how to retrieve that stored information, and we haven't really gone into any detail on storing or retrieving data from the database, yet.
Currently, I have the first form, which has a list of radio buttons and items, like so:
<form action="shoppingCart.php" method="post">
<input type="hidden" name="step" value="1" />
<table id="shoppingList">
<tr class="d0">
<td class="rad"><input type="radio" name="items" value="oolongTea"></td>
<td class="item">Oolong Tea</td>
</tr>
...
This list continues on like this for 10 items. When I click the next button, it submits that data to the form, and then proceeds onto the next page. Where does that data go, I suppose is my question, and how would I go about getting it back?
I should add that I also have this block at the top of my code, for checking which form to display:
if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"] <= 3 ) {
call_user_func( "processStep" . (int)$_POST["step"] );
} else {
displayStep1();
}
And this block is the block that handles the "processing" of the first form:
function processStep1() {
$_SESSION["items"] = $_POST["items"];
displayStep2();
}
The data submitted for
<input type="radio" name="items" value="oolongTea">
can be found in
$_POST["items"];
so if you selected that radio button and submitted the form, $_POST["items"] would be equal to the value attribute on that input tag ("oolongTea")
I am trying to create a registration website where the users chooses amongst three options on the first page and after selecting, can move onto the second page where it is displays different information depending on the option selected previously.
On Registration_1.php this is the code:
<?php
$clicked = $_POST["Next"];
if(isset($_POST['Reg_type']))
{
header('Location:Registration_2.php');
}
elseif(isset($_POST['Next']))
{
header('Location:Registration_1.php');
echo "Error! You must select an option!";
// display form again here
}
?>
<form name="frmtype" action="Registration_2.php" method="post" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Next" value="Submit"/>
</form>
How can I redirect the user back to this page if they simply click submit without choosing an option and possible display a error message, and send them to page 2 if they choose an option? Thank you
Edit: Added if block, but still moved to next page regardless of selecting an option or not. Is the header() not the correct method to move to another page?
if(isset($_POST['Reg_type']))
{
// execute some code
// go to page 2
}
else
{
echo "Error! You must select an option!";
// display form again here
}
Documentation: $_POST variable
I'm asking this question in regards to my friend, so I do not have code samples to post here at the moment. Hopefully I'm clear enough that someone can help.
So he has a simple contact form except it has multiple checkboxes that the user can choose to send their requests to multiple recipients... like so...
x I would like to know about flight school
x I'm interested in becoming a teacher
x I would like someone to contact me about your degrees
Name
Email
Comments
And so based on which checkboxes are selected it should add that recipient to the email function so that they receive the users comments and interest.
The form is validated by jquery and uses the $.ajax function to POST the Name, Email and Comments fields over to a process.php... we are validating that at least one of the checkboxes is selected, however, we haven't been able to figure out how to pass its boolean value to the process.php and in-turn add the relevant email address to the mail() function.
I do realize this is semi-vague without posting our code, but I don't have access to it right now... and I have been searching google for about 30 minutes trying to find something to work with. Any help would be appreciated. Thanks.
you can simply check if the value you got is true or not:
basic idea :
if(checkbox-1-ischecked)
//send email to first recipent
end if
if(checkbox-2-ischecked)
//send email to 2nd recipent
end if
if(checkbox-3-ischecked)
//send email to 3rd recipent
end if
if(checkbox-4-ischecked)
//send email to 4th recipent
end if
Etc
This would seem to answer you check box query. (http://stackoverflow.com/questions/908708/how-to-pass-multiple-checkboxes-using-jquery-ajax-post)
In basic terms it would post an array back to the php script which you could then parse and depending what was ticked / the vars passed back you could then append more email addresses to the 'to' part of the mail function.
For a simplier implimentation you could just keep your three check boxs seperate not in an array and ajax post them back individually.
HTML
<input type='checkbox' name='flight' value='1' id='flight' />
<input type='checkbox' name='teacher' value='1' id='teacher' />
Then simply on the server via PHP
$to="";
if($_POST['teacher'] == 1) {$to = $to."joe#email.com,"};//append email
if($_POST['flight'] == 1) {$to = $to."bob#email.com,"};//append email
$to = rtrim($to, ","); //remove trailing comma
NOTE as with all web to mail scripts make sure you sanitize all vars to prevent spam abuse!
Name your elements as an array like this:
<input type="checkbox" name="mybox[]" value="foo#example.com">Foo</input>
<input type="checkbox" name="mybox[]" value="bar#example.com">Bar</input>
<input type="checkbox" name="mybox[]" value="hello#example.com">Hello</input>
<input type="checkbox" name="mybox[]" value="world#example.com">World</input>
After POSTing the form to your PHP, $_POST['mybox'] will be an array holding the values of the boxes checked.
In your PHP
if(isset($_POST['my_box']))
{
$subject = "sub";
$body = "body";
if (is_array($_POST['mybox']))
{
//multiple items were selected.
$to = implode(',',$_POST['my_box']);
mail($to,$subject,$body);
}
else //only one item was selected
{
echo $_POST['my_box'];
$to = $_POST['my_box'];
mail($to,$subject,$body);
}
}
else
//none were selected
You can simply assign the same name to all checkboxes, which actually results in a checkbox array.
<form name="someform" onsubmit="return validate(this)" action="process.php" method="post">
<input type="checkbox" name="names[]" value="Saji">Saji
<input type="checkbox" name="names[]" value="Muhaimin">Muhaimin
<input type="checkbox" name="names[]" value='Muhsin'>Muhsin
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
In process.php, you could have-
$name_val=$_POST['names'];
foreach($name_val as $values){
//Here $values will contain only the values of the checkboxes you had selected.
echo $values."<br />";
}
i have an html form full of text fields, checkbox's , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn't necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check isset() first.
For a checkbox:
$data['my_checkbox'] = isset($_POST['my_checkbox']) ? 'on' : 'off';
and for a radio button:
$data['my_radio'] = isset($_POST['my_radio']) ? $_POST['my_radio'] : false;
To be a little more descriptive let's say you have the following form:
<form action="test.php" method="post">
<input type="text" name="email" value="" />
<input type="checkbox" name="active" value="Yes" />
<input type="submit" value="Submit" />
</form>
If I were to submit that form with an email value of 'test#email.com' but not check the checkbox I would have the following in $_POST:
Array (
'email' => 'test#email.com'
)
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Array (
'email' => 'test#email.com',
'active' => 'Yes'
)
Hope that helps.
0./ Try using the following code to see the raw posted data:
echo '<pre>';
print_r($_POST);
echo '</pre>';
1./ Make sure you use a name attribute value for your checkbox and radio inputs.
Typically for checkboxes, it will be an array.
<input type="checkbox" id"=fruit-apple" name="fruits[]" value="apple" />
<input type="checkbox" id="fruit-pear" name="fruits[]" value="pears" />
2./ Make sure they sit inside the form tag.
3./ If you submit using a javascript call, try disabling javascript and see if the error stays. If it does not, you know your javascript is the culprit.