I have form with a submit button that changes value depending on the page. When the submit button is pressed, I need that value to do some data validation. However, the value is being lost somewhere in the process.
I did a value check on the button using 'click' in jQuery that fires before the 'submit' of the data. The value is correct.
On the page load, I tried checking the POST variable value and it is gone.
EXCEPT in the following situations:
If I do NOT use the POST variable in any way, such as 'if' statements. Or assign its value to a variable. If I do any of those things, the value is lost does not even show up when I check for its value at the top of the page.
2.If I leave one of the required fields in the form blank.
I should note that this is a Wordpress site. I know people are going to be asking for code, but the page is quite long, so I will try to get some code here soon.
Submit buttons do not submit their values, add an input type='hidden' with the value
Add an input hidden and set its value to the value of the submit button right before pressing the submit button:
$(document).on('submit', '#form', function(e) {
e.preventDefault();
$('#form input[name="submit_value"]').val($(this).find('input:submit').val());
$(this).submit();
});
HTML:
<form method="post">
<input type="hidden" name="submit_value" />
<input type="submit" value="Submit" />
</form>
I finally found the error. Turns out it didn't have to do with the POST.
There was an extra line in the code that kept resetting a variable back to the initial value.
Sorry for the trouble and thanks for the help anyway!!
Related
I have an input like this:
<input value="<?php echo $formdata['title'] ?>" type="text" name="title" id="Editbox2">
This is an edit page, I load database data into fields with echo, replace them, and hit submit to update them.
But when I hit submit it refreshes the old data onto browser's fields, how can I prevent this?
Submit your form using ajax request with jquery submit.
Use action="javascript:;" for the form tag
You need to handle the script with javascript, then prevent the default behaviour, which is refreshing the page. Here is an example:
*I haven't tested this, but from what I recall this is what I used to do. Let me know if it doesn't work, I'll give other suggestions.
<form>
<!-- elements inside -->
<input type="submit" id="submit-btn" value="Submit"/>
</form>
and in your javascript have the following:
<script>
$("#submit-btn").click(function(e){
e.preventDefault();
// handle form here with your JS
});
</script>
I am pretty new to web programming and I cannot figure this problem out. To keep things simple, say I have 2 pages. The first page has a form with two selection boxes and a submit button. The second page has a form with two text input boxes and a submit button. After the form on the first page is submitted it goes to the second page and the two text input boxes are filled with the values from the first form with a $_POST.
My problem is, when I submit the second form (which goes to the same second page on submit) the $_POST variables of the form are empty.
I thought the if-else in the value would fix it. The purpose of that was because after submitting the $_POST from the previous page no longer has a value and I want the value in this forms field to be displayed after submitting (which is still the same value from the first form). Not only do I want it to display the value in these fields, I want to use them for a database query (which is why them being blank is a problem).
The values in the form when the second page is reached are correct. Also in the else case if I echo "test" instead of the $_POST it is displayed in the box so I believe I have it narrowed down to the $_POST being blank after submitting but I have no idea why.
<form method="post" action="newSerialNumber.php">
JON: <input type="text" name="newSNJON" value="<?php if ($_POST['JON'] != ""){ echo $_POST['JON'];}else{ echo $_POST['newSNJON'];} ?>" disabled>
Part Number: <input type="text" name="newSNPN" value="<?php if ($_POST['PN'] != ""){ echo $_POST['PN'];}else{ echo $_POST['newSNPN'];} ?>" disabled>
<input type="submit" name="Button" value="Add">
</form>
You have the disabled attribute set in those input fields.
Disabled form elements are not send when the form is submitted.
http://www.w3.org/TR/html5/forms.html#attr-fe-disabled:
“The disabled attribute is used to make the control non-interactive and to prevent its value from being submitted.”
If you don’t want the user to be able to change the values, but still send them with the form when it is submitted – use the readonly attribute instead.
Yes, it's right.
Disabled fields are not included in the submit. Remove the disabled attributes and you can see it works.
I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>
Given this simple javascript function:
function update_cart() {
alert("--update--");
document.form1.command.value="update";
document.form1.submit();
}
and this button at the bottom of my form:
<input type="submit" value="Update" onclick="update_cart()" />
The data in the form is 'submitted' to the new URL. but the update_cart function is never called. I know because I never get an alert box, and the URL reads...?c=Albania&m=....
Also, the form element
<input type="hidden" name="command"/>
does not get posted to the URL, either. URL reads ?command=&c=Albania...
I have tried the following: changed onclick to onsubmit, checking $_REQUEST variables, cutting and pasting the code from known working pages.
I'm at my wit's end, and would be grateful for any help!
Oh, yes: same behaviour in firefox 6, Opera 11.5, & IE7. I'm running WinXP SP3.
Thanking you,
Sadhu!
Once try
<input type="button" value="Update" onclick="update_cart()" />
instead
<input type="submit" value="Update" onclick="update_cart()" />
if you want that for 'submit' type then go with 'Allen Liu' answer
enter code hereif you wane to change sth when onsubmit, you need to do these changes before form's submitting. so you need to add these opeartion to the "onsubmit" event of the form, rather than the "onclick" event of the submit button.
like this:
<form name="toSubmit" onsubmit="update_cart();"><input type="submit" name="btn" value="hello"/></form>
If you want the script to run on submit of form, use:
<input type="submit" value="Update" onsubmit="update_cart()" />
The onsubmit event is usually used to run some validation script. If the validation returns true, the form will submit. If it returns false, the form does not submit. In your case, the script is coded to submit the form so no return boolean value is necessary.
Otherwise, I would not give your button the submit type if it really doesn't submit the form. You can simply use button tags with the onclick and that should work:
<button onclick="update_cart()">Update</button>
First of all there is no need to change your html; see my demo.
On every click on a submit button, first the click-handler (if exists) will be started, then the submit-handler (if exists) (which should be in the form tag) and then the action of the form will be executed. This procedure will be only stoped, if a handler returns false.
But why will your javascript function update_cart not be called?
I think it could not be found, but I don't why. Can you bind the function to the window dom element only for testing (like in my demo)?
P.s.: you don't need to submit the form in your click-handler (if you don't return false). You can remove the line: document.form1.submit();.
P.s.: it will be better not to use a click-handler on the submit-button, instead use a submit-handler in the form tag (see my demo2).
Here is my scenario, I present the user with a table of tests, which I have retrieved from my database in a loop and created a form for each test(row of table). So each has a submit button to execute that particular test.
basics of my loop:
while ($ts = mysql_fetch_assoc($test_info))
{
//PRESENT VALUES $ts['name'] in table within a unique form etc.
}
What I am trying to do and failing is, on clicking a particular submit button for a test, to run a JS function which checks; if the test has a password attached, if it does, present a popup form for password input, and on submitting that small form check if password is correct, if it is submit the original test form.
My problem is that I cannot parse the password value attached to that form to my javascript.
so ideally i want to do this:
<input id='submit' type='button' onclick='JSfunction(test_password)' value='execute test' >
So I can somehow parse a value from that particular form to a javascript function without actually submitting the form.
and I believe I know how to do the rest in my JSfunction.
I hope somebody can follow my poor explanation and point me in the right direction.
Many thanks,
When a form should have a password associated with it, add the following:
<input type="hidden" name="has_password" value="yes" />
<input type="hidden" name="password" value="" />
Then, in your check triggered by the submit button (assuming the button itself is the this context):
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'yes') {
// pop password request
return false;
}
You'll need a way to store the context of the current form, and I can suggest a few if you like, but you can then populate the password into the hidden field for it and submit as normal. By the way, you might want to consider onSubmit instead of the submit button's onClick, as onSubmit will catch attempts by scripts to perform a submit. If you do this, just remove the .parent() portion of the above, as the <form> element should be the this context.