I am creating a order form for a meat locker company that sends the form to their email. There is a lot of where if you select one item you can not select another item; such as t-bone and New York Strip, you can have one or the other, but not both. Here is what my code looks like.
<P>T-bone steak <input type="radio" name="T-bone and NY" id="T-bone steak" />
or New York Strip steak <input type="radio" name="T-bone and NY" id="New York Strip" /> </p>
This does prevent a person from selecting both, but when he views in in his email he sees name="T-bone and NY" so he doesn't know which one they selected. I thought it would display the id="T-bone" or "NY Strip". I'm sure there is a better way of doing this with an if statement.
Second, when they receive the email it has all of the names of the fields people selected and it says on afterwords. For example if someone selected Prime rib the email would say:
Prime Rib: on
Is there a way to send the form exactly as the user sees it. Maybe in an image, word doc, or access data base?? I'm open to anything.
If your want to see it the url is www.spillvillelocker.com/beef.php
Thank you very much:)
Your code should look like this:
<P>
T-bone steak <input type="radio" name="steak" value="T-bone steak" id="tbonesteak" />
or New York Strip steak <input type="radio" name="steak" value="New York Strip" id="nystrip" />
</p>
Notice, I have "name" be the same for both, with different "values". Also, "id" attributes shouldn't contain spaces, so I changed them to something alphanumeric only.
I think you just need to provide a VALUE="" attribute to show which value each radio button specifies... also NAME and ID should be equal.
<P>T-bone steak <input type="radio" name="MEAT1" id="MEAT1" value="TBONE"/>
or New York Strip steak <input type="radio" name="MEAT1" id="MEAT1" value="NYSTRIP"/> </p>
You need to set value attributes of those inputs.
BTW you should not have spaces in name attributes.
In addition to what Mike posted, regarding your second question "why the email says 'Prime Rib: on', it is again because there is no value attribute assigned to the element. The default values for radio buttons are 'on' and 'off'. Without setting an explicit value, you're left with the defaults.
There are two things that you can do for this.
Set an explicit value for the form element, something like
<input type="radio" name="primeRib" value="Yes"> Prime Rib
That will show up as 'primeRib: Yes' in the email.
The second thing you can do is use PHP to parse your responses to only send the data you want. Something along the lines of:
<?php
foreach ($_POST as $key=>$value) {
if ($value=="off") {
continue;
}
else {
$email.="$key\n";
}
}
?>
Something like that would skip over each $_POST variable that is set to "off" and for each variable that isn't, it would add the $key (the data inside the "name" attribute) to the $email variable, or however you add the data to the email to be sent out. There's a number of things that you can do, but at least this would allow you to filter only for variables that are on/checked.
Related
I am posting this because I do not really know what I could search for. Please note that I have basic knowledges and am still learning ; also, I might sometimes use wrong terms, sorry about that.
I am setting up a web application through PHP/MySQL, HTML, Bootstrap, and JQuery.
I need to post an array to my database. The problem is, this array might contain several times the same value. Indeed, the array is made out of values inside a html table, to which rows can be added to add more data at the same time thanks to JQuery and DataTable.
So far, I did not even know how to post an array, and found the following solution to be working to do so, as the following code:
<form method="post">
<input name="country[]" value="France">
<input name="country[]" value="Spain">
<input name="country[]" value="Germany">
<input type="submit">
</form>
$country = $_POST['country'];
print_r $country;
Would contain the following array result:
|country|
|-------|
|France |
|Spain |
|Germany|
Wonderful.
But what I need to do sometimes, is something like this:
<form method="post">
<input name="country[]" value="France">
<input name="country[]" value="France">
<input name="country[]" value="France">
<input type="submit">
</form>
And I would like this:
$country = $_POST['country'];
print_r $country;
To contain the array:
|country|
|-------|
|France |
|France |
|France |
But instead, the POST contains only
|country|
|-------|
|France |
Whereas Firefox Developer Tools clearly indicates me that the page sent the exact array I need to receive.
Form Data
country[]: [...]
0: France
1: France
2: France
Could you please help me with that?
Thank you very much for your time and attention.
Thank you Daan for asking me how I checked what $_POST['country']; contains.
I modified my original post to include these information. By doing so, I saw that I checked it once I changed it, as I use it again later in a foreach, to display all the rows in another array.
So I checked it the right way.
It turned out my problem was a lot more different, and is related to my use of the foreach.
Indeed, I use the country name as an index for the foreach. As it can only use unique indexes, it uses only once "France".
To scaisEdge: This is data I modified to be anonymous, and country was the easiest way to do so. Imagine products instead of countries, and this is why the user can add several times one unique name.
I've created a database-driven test with three types of questions - input type = radio, text and checkbox. If I insert required before the closing tag, it works fine for the radio and text questions, but not the checkboxes.
The problem is that each checkbox question has several possible answers - anywhere from three to six. And the number of those choices that are correct answers also varies.
But when I add "required" to the checkbox script, the user has to select EVERY answer in order to proceed to the results page. This is what the code looks like:
<label for="q'.$QID.'-'.$Value.'"><input type="checkbox" name="q'.$QID.'[]" id="q'.$QID.'-'.$Value.'" value="'.$Value.'" **required**> '.$QA.'</label>
I found the HTML5 Required Attribute page, which appears to address this problem. If I understand correctly, you can fix the problem by inserting "required" in just one element. So if you insert it in answer A, a user could presumably choose answers C and D, and everything would work just fine.
If that's correct, then is there a way to make "required" display in just one checkbox, instead of all of them?
Use required using a condition for the first checkbox of a group. I assume your html code is written in .php files.
$required = ($Value == 0) ? "required" : ""; //condition to check if the value is first. since it is database driven it would be better to check the key instead of the value.;
echo '<label for="q'.$QID.'-'.$Value.'"><input type="checkbox" name="q'.$QID.'[]" id="q'.$QID.'-'.$Value.'" value="'.$Value.'" '. $required .'> '.$QA.'</label>';
Another solution is to add required attribute using Jquery. For eg if gender is the name of the group of checkboxes, then:
$('input[type=checkbox][name=gender]')[0].attr('required', 'required');;
given a form like the following
<form action="/page" method="POST>
<input type="hidden" name="input" value="12" />
<input type="hidden" name="input" value="24" />
</form>
Using Google Chrome 31.x and PHP 5.5, /page now has a $_POST variable for input of 24
This happens because when the $_POST array is created, The value is over written in the array. And the latter value is the value which is preserved.
Most browsers Ive tested this is the case, But Is there any HTTP spec / browser spec which says that form inputs should be sent in the order they are defined ? Or could an update in the future (or an old browser) send these updates in the reverse order for example ? or a random order ?
Edit:
to give more context, It will not be used like the above in all cases. only in a certain case.
The first form element is a SELECT box, But depending on the options chosen, Javascript will be able to change the value, Without changing the Select box value
Regardless of the order in which HTTP sends your two values, PHP can only have one value for $_POST['input'].
To solve this, use array notation:
<form action="/page" method="POST>
<input type="hidden" name="input[]" value="12" />
<input type="hidden" name="input[]" value="24" />
</form>
Now you'll have an array $_POST['input'] with both values.
To answer your question about the spec, see this page:
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
See the bullets for the application/x-www-form-urlencoded default content type.
The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.
To my knoweledge there is no specification as to the order in which a browser should parse the from before submitting.
But I would say that you can pretty much assume that the form fields will be parsed from top to bottom, because the whole dom is parsed like this.
Here is a little bit additional information as to how a form submit is processed/handled.
http://www.w3.org/TR/html401/interact/forms.html#successful-controls
Steve
I have read through many questions and do my searches for hours, but i still cannot find the solution to what i exactly want.
<form method="POST">
<input type="checkbox" name="fruit" value="0" />No Preference
<input type="checkbox" name="fruit" value="1" />Apple
<input type="checkbox" name="fruit" value="2" />Orange
<input type="checkbox" name="fruit" value="3" />Banana
</form>
print_r($_POST);
I already did validation to checkbox value 0 to 1,2,3 so they will inverse and i did it using looping javascript search for the element name. The problem is, i have to use the element name without changing it become array name. (e.g Fruit => Fruit[] ). So i need to use this element name to retrieve all checked information inputted by the customer. I've seen this can be done in ASP, but i could not figure how they do as it's long time ago already.
My question is, could any one figure how to do this without changing the element name into array format (e.g Fruit => Fruit[] ) ? T.T
Any help will be appreciated. Thank you..
As you already figured, when you submit the same name multiple times, the latest one will overwrite former ones. A solution is to set up a hidden field
<input type="hidden" value="" name="foobar_as_array">
and use jQuery or plain JS to concat your values into that field on submit
<form ... onSubmit=$('#foobar_as_array').value(...concatenate them...);">
I changed the validation using this line :
var GenBox = eval("document.forms['SubmitSearchAll']['" + oCheckBox+"[]']");
and it helps me alot. Now it is solved.
Thanks everyone for contributing.
I was going to use jQuery to clone the input field when I click a button to make another field, but I was thinking of doing:
Page One, Page Two, Page Three <- then send that as one $_POST to the server and have it take each page and break on the "," comma then for each insert in to my POSTS table.
Any idea on how I would do that? Would I use explode()? Then inside a foreach() run a query for each item in the field.
So if there were 5 pages typed in separated by commas, it would run five times using the foreach().
for each(){
EXECUTE SQL HERE
}
Does that sound like it would be the best way of going about it?
If you set the name attribute of the input element to the name of an array, e.g. foo[], you'll be able to access it as such when processing the $_POST vars, e.g.
<input name="foo[]" type="text"/>
<input name="foo[]" type="text"/>
becomes two iterations of:
foreach ($_POST['foo'] as $foo) {
// process $foo
}
explode and foreach sound good.
But as Click Upvote indicated: is it for pagination? 'cause then there could be a betetr way to achieve this.
You can do the following:
<input type="text" name="list[]" />
<input type="text" name="list[]" />
<input type="text" name="list[]" />
<input type="text" name="list[]" />
Everytime you need a new input box you can do
$(some_element).append( '<input type="text" name="list[]" />' );
or sth similar with jQuery.
Then you have an array instead of a single value in your PHP $_GET or $_POST variable. So you can use $_POST['list'] in foreach.
I was thinking, I can type in:
Home, Products, About, Contact
Into a input field, then my php can break it apart using the comma and insert each of those page titles in my PAGES table.
Run the SQL within the foreach.
Thoughts?