I prepend
$('.VHere').prepend('<input type="checkbox" value="test1" id="testid">');
And write into the DB. When posting via form, the does not capture #testid. It capture every other field in the form except for this. Also if the page is reloaded this particular field is captured.
Any idea why.
We need to see more code, but there is no name associated with this input, which may be why the content is not being added to the DB.
I would think it would be
$('.VHere').
prepend('<input type="checkbox" name="testid" value="test1" id="testid">');
where testid populates the field
may be this works?
$('.VHere').prepend('<input type="checkbox" name="testid" checked="checked" value="test1" id="testid">');
because unchecked box cannot be submitted with post as AD7six says and name is of course important to post and catch back..
Related
I have a checkbox in Wordpress, which is validating properly until the user doesn´t check the box.
Then it will remain acting as unchecked and will not allow to login again, showing allways the oninvalid message (no matter if it is checked or unchecked).
<input type="checkbox" required id="accept_terms" name="accept_terms"
class="validate[required]" value="1" oninvalid="this.setCustomValidity ('Please...')">
I have also tried with onchange instead of oninvalid. Same problem.
It only had a way to change validation state, adding validity.valueMissing solved it.
onchange="this.setCustomValidity(validity.valueMissing ? 'Please...
I have the following input in a form
<input name="email" type="text" id="email"size="50" english="Email address" />
I have a custom tag called english, My question is can I send this as post data and can I recover it on my new page ?
Any help would be much appreciated , Thanks
If you use JavaScript to submit your form, you can read you custom tags' values ad append them to the form data to send. Otherwise, clean HTML form just submits only input tags value.
The best method I can think of right now is to have hidden field with the label as value. Like
<input name="email_label" type="hidden" id="email_label" value="Email address" />
The short answer is: no. The post data received from the HTML in your question will be an array with email as the key, and whatever the user typed as the value.
The solution depends on the problem you're trying to solve. Consider using a hidden input tag instead. For example:
<input name="language" type="hidden" value="English" />
Alternatively, a neater solution would be to store the language in the session (assuming that does what you need). You should never rely on the front end of a website "telling" the back end stuff like this, at least to a certain degree. The back end should just "know".
i have following part of code:
<input id="add" class="add" data-amount="150" data-monthly="25" type="checkbox" name="accessory[]" value="Accessory Name"/>
wondering if there any way to post all data in "value", "data-amount" and "data-monthly" together to another script?
If you are writing the value of those attributes onto the page server side, you are better off working off of a key and retrieving the values again server side based on the key. The HTML could be altered resulting in non-trustworthy data being send back.
For example...
<input id="add" class="add" data-amount="150" data-monthly="25" type="checkbox" name="accessory[]" value="1"/>
Where 1 is the id of the data that contains the amount and monthly data.
To get all those values in the checkbox, you can concatenate them together separated by some delimiter and then split the value when processing the form.
<input type="checkbox" name="accessory[]" value="Accessory Name*150*25"/>
This is not an elegant solution. It does what you ask, but you should strongly consider Sohnee's answer as a better method of doing what you want to do.
Data attributes are not submitted with form data.
No there is not, however you can put all the attributes you need in hidden fields which are passed with form.
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?
I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.
The better approach is to remove the hidden field, and simply have a check in PHP:
if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }
You shouldn't need the hidden field. You should in fact not trust any of the form fields sent in the first place. What this means is that you cannot make code which takes the sent fields and trust them to send the correct data (which I assume you do now).
What you should do is to handle all fields you expect to get. That way if you don't get the checkbox value you can still handle that as if it was unticked. Then you also get the added inherent feature of throwing away form data you don't expect in the first place.
No, it will pass all the form data, whatever it is. The right way to do this is not to set the checkbox via a hidden field but to set the checkbox with whatever its state actually is!
I mean... why are you adding the hidden field to begin with?
Your PHP is receiving two fields named check_box_1, and last time I checked there was no way to guarantee that the params would get read into the REQUEST hash in the exact same order as you sent them, so, there's no way to tell which one will arrive last (that's the one whose value will get set). So... this is not the right approach to whatever problem you're trying to solve here.
Welcome to Stack, btw! If you find answers useful or helpful, make sure to mark them as correct and vote them up.
That's normal.
They must be both type="checkbox" to pass only 1 value.
If you want to get only 1 in any cases you can do:
<input type="checkbox" style="display:none;" name="check_box_1" value="0">
Make sure the first input field is of type Checkbox, or else it won't behave like one.
<input type="checkbox" name="check_box_0" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
Everything is working normal with your code so far.
I'm assuming you are creating the hidden field so that 0 is passed to the server when the checkbox is not checked. The problem is that they both get passed when the check box is checked.
As Death said, the way you should be doing it is with a single checkbox and then checking if the value has been sent to the server or not. That's just how checkboxes work.
If you want to have a default set then you will have to handle all that on the server side based on weather the checkbox has a value.
For example:
$myValue = "";
if(isset($_POST['check_box_1']))
{
$myValue=$_POST['check_box_1'];
}
else
{
$myValue="0";
}
is there a way to create custom post variables when a user presses submit, like this:
$_POST['var'] = 'hi';
In order to set post values on the page with the form you should use hidden input tags.
i.e.
<input type="hidden" name="var" value="hi" />
It will be invisible and your receiving script will see that key/value passed along.
Variables POSTed by the browser to your PHP script will only correspond to the fields of the form that was used in the browser -- which means you have to put your custom data in that form.
If you don't want them displayed, you can use a hidden input field :
<input type="hidden" name="var" value="hi" />
But note that the data will still be sent by the browser -- which means you have to escape/filter/protect it, like any other value that comes from the user ; and it cannot be trusted : anyone can pretty easily modify the value of that form field, even if it's not visible.
while $_POST variable is an array, you can also define var like this
$_POST['var'] = 'hi';
it is same like hidden field. :)