Issue with POST values in jQuery dialog - php

<form>
<input type='submit' name='btn1' value='btn1' onclick='javascript:return test()' />
</form>
function test(){
$('#testDiv').dialog('open');
}
Dialog have 2 buttons 'OK' & 'cancel'
On the 'OK' button I have written .submit() which is working fine.
But the issue is when values are posted to .php file $_POST['btn1'] is not set. Which would have been set if there is no function called on OnClick event and the submit button had been directly triggered.
Can anyone help?

yes, if you do manual jQuery submit , i.e) by calling $("#formid").submit(); your submit button will not present in POST values but all other values will present.
This Issue I have noticed while am working on jquery Ajax captcha.
You can confirm that by putting one or two hidden values and check your POST values.

put some value in your submit button.
<input type="submit" name="submit" value="submit" onclick="... do whatever"/>

Related

AJAX submit function with alert does not work

im trying to get an ajax submit function to work. i cant find what the issue is here. the button for submit is inside a table inside a form
i tried using alert to see if its working but it still not.
the method-
$("#submit").submit(function(){//comparing the total quoted hours with duration of time selected
alert("DEMO TEST");
// code for the comparing values goes here but that's another
//story
});
the submit button-
<input type="submit" name="submit" value="submit" id="submit" onclick="index.php">
obviously the alert should pop an alert with DEMO TEST.
i dont know where im going wrong
It just because of you are calling submit event. When you call submit event your form will submit. you need to take id and class of form like.
<form action="" id="formsubmit">
<input type="submit" value="Submit"> //Remove name="submit" onclick="index.php"
</form>
$("#formsubmit").submit(function(){
alert("DEMO TEST");
});
You have to point the .submit to your form, not your button.
More info on how submit works on jQuery https://api.jquery.com/submit/
jQuery on click event is getting triggered first as onclick="index.php" so remove it and submit the form then it will work.or if you want that event to be trigerred you should return true. or you can submit form using jQuery using .submit() and after submitting the form you can use location.href

POST Variable Lost on Submit

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!!

Basic Javascript: onclick event not working

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).

Using custom buttons with js & php

How can I make a form with my own "submit" button & post the info to a php server side?
how can I get js to post it?
You can use the submit() method to submit forms via javascript:
document.form_name.submit();
Or:
document.getElementById('form_id').submit();
Or:
document.forms[0].submit(); // submit first form on page
document.forms[1].submit(); // submit second form on page
If you don't want to use a submit button and want to use a normal button, you just need to put any of the above code in onclick event of that button.
<form id=myForm action=process.php>
<input name=text>
<INPUT TYPE="image" SRC="images/submit.gif">
</form>
To submit
document.getElementById('myForm').submit();

How to check if a form is submitted via javascript?

I have this conventional submit button which submit a form like this:
<form method="post" id="form_submit">
...
<input class="button" type="submit" name="Submit" value="Submit">
</form>
And I check if the submit button is clicked using this:
if(isset($_POST['Submit'])){
//update DB
}
Now I have a submit link using jquery:
Submit
JS code:
$("#form_submit").submit();
What is the alternative way here to be used here for if(isset($_POST['Submit'])) since I'm submitting the form using javascript?
If I understand you correctly, try this:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// your code.........
}
You should add a hidden input <input type="hidden" name="formsubmit" value="yes" /> to the form which will always get submitted, and check for that instead of the button (which only gets submitted if it is clicked on ..)
If I understood your problem correctly that you can simply change input type to hidden.
<form method="post" id="form_submit">
...
<input type="hidden" name="Submit">
</form>
$_POST['Submit'] variable will be defined.
The best solution is "Don't do that". If you want to submit a form then use a submit button (don't do it as a side effect of clicking on a hyperlink to the top of the page). Any JavaScript you want to run can then be handled in the form's submit event.
If you really want to do it as a side effect, then check for the existence of any other field that you know will be set. You could add a hidden field to ensure there will be one of a given name/value combination if you like.

Categories