Probably a very simple answer but one I am struggling with. I have a basic form which has a standard submit button. When this is clicked, it is being valued by jquery serialize and then Ajax to process php page etc. I am confused as to why the name of my submit button 'submit' is not being posted when a user clicks on it. The only reason I can think of, is that serialize only handles text,text area and select elements. If so, how can I send the name through to php for processing. I have included some sample code and would be grateful if someone could point me in the right direction. Thanks
html form
<!--- Form to add box -->
<div id="boxaddform" style="display:none;">
<div class="dialogTop_padd"></div>
<form id="BA_boxform" name="BA_boxform" method="post">
<fieldset>
<legend><span></span>Select Company</legend>
<div class="spacer"></div>
<div class="formMessage">Click again to open</div>
<div class="fld_fld">
<div>
<label for="BA_customer">Company:</label><br />
<select name="BA_customer" id="BA_customer">
<option SELECTED VALUE="">Select a Company</option>
<?php
do {
?>
<option value="<?php echo $row_Recordsetcust['customer']?>"><?php echo $row_Recordsetcust['customer']?></option>
<?php
}
while ($row_Recordsetcust = mysql_fetch_assoc($Recordsetcust));
$rows = mysql_num_rows($Recordsetcust);
if($rows > 0)
{
mysql_data_seek($Recordsetcust, 0);
$row_Recordsetcust = mysql_fetch_assoc($Recordsetcust);
}
?>
</select>
<div class="spacer"></div>
<!--- displays the address and dept from the change function -->
<div id="BA_dept"></div>
<br />
<div id="BA_address"></div>
</div>
</fieldset>
<div class="dialogTop_padd"></div>
<!--- fieldset for service level -->
<fieldset>
<legend>Service Level</legend>
<div class="spacer"></div>
<div>
<label for="BA_service">Service level:</label>
<select name="BA_service" id="BA_service">
<option SELECTED VALUE="">Select an option</option>
<option value="Standard">Standard</option>
<option value="Rapid">Rapid</option>
</select><br />
</div>
</fieldset>
<div class="dialogTop_padd"></div>
<!--- fieldset for box # -->
<fieldset>
<legend>Box Details</legend>
<div class="spacer"></div>
<div>
<label for="BA_box">Box#:</label><br />
<input id="BA_box" name="BA_box" type="text" size="32" maxlength="128" /><br />
</div>
<div>
<label for="BA_destdate">Destroy date:</label>
<input id="BA_destdate" name="BA_destdate" type="text" size="32" maxlength="128" value = "" /><br />
</div>
</fieldset>
<div class="dialogTop_padd"></div>
<!--- fieldset for authorisation -->
<fieldset>
<legend>Authorisation</legend>
<div class="spacer"></div>
<div>
<label for="BA_authorised">Requested By:</label>
<input id="BA_authorised" name="BA_authorised" type="text" value="<?php echo $_SESSION['kt_name_usr']; ?>"><br />
</div>
</fieldset>
<!--- div to show callback result from ajax via dialog -->
<br />
<div id="BA_addbox"></div>
<br />
<input class="AddBoxSubmitButton" type="submit" id="submit" name="submit" value="Add Box" />
<input class="AddBoxResetButton" type="reset" name="cancel" value="Clear Form" />
<!--- buttons to submit form and reset form to default status -->
<!-- <button id="BA_submit" class="submitbutton icon-right ui-state-default2 ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Add Box</button>
<button type="reset" id="BA_reset" class="resetbutton icon-right ui-state-default2 ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Reset</button>
--><br />
</form>
<br />
</div>
jquery code
$(function(){
$("#BA_boxform").submit(function(){
var formdata = $('#BA_boxform').serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "/domain/admin/requests/boxes/boxesadd.php",
data: formdata,
dataType: 'json',
success: function(msg){
//$("#confirm_department").hide();
/*
var $dialog = $('<div id="dialog"></div>')
.html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'Box intake submission successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
*/
//alert('You have succesfully submitted your ' + msg.company + ' report. Thank you.');
//console.log(msg);
$("#BA_addbox").show();
//$("#formImage .col_1 li").show();
$("#BA_boxform").get(0).reset();
//$("#boxaddform").hide();
}
});
return false;
});
});
// End function to submit box intake form
cutdown php code for illustration
if (isset($_POST['submit'])) { }
jQuery won't serialize submit button:
Docs:
Note: Only "successful controls" are serialized to the string. No
submit button value is serialized since the form was not submitted
using a button.
Workaround is to check if $_POST is not empty:
if (!empty($_POST)) {
// Your code here
}
or check if specific input was set. E.g.
if (isset($_POST['BA_customer'])) {
}
Like said, submit buttons aren't serialized. A possible workaround: { not tested but as i understand it, should work }
$('#BA_boxform input:submit').on('click', function () {
var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val();
//....
return false;
});
Related
Keeps going to next page but I want it to print below the form box. Any ideas why this is happening?
<input type="text" name="name" id="name" placeholder="Enter">
<div id="result"></div>
<input type="submit" value="Submit">
<!-- Placeholder where link is updated on success or an error message is shown -->
<div id="result"></div>
</form>
</div>
</body>
<script>
$('#process_form').ajaxForm({ // Make sure the form doesn't redirect us.
success: function(response) {
// Update the link with the response.
$('#result').html(response);
$('#name').val('');
}
});
</script>
This is how you can solve this problem...
just use this Elemetn.preventDefault()
here your html code
<form action="" method="get"></form>
<input type="text" name="name" id="name" placeholder="Enter">
<div id="result"></div>
<input type="submit" value="Submit">
<!-- Placeholder where link is updated on success or an error message is shown -->
</form>
here your js code
$('input[type="submit"]').on('click', function (e) {
e.preventDefault()
var value = $('#name').val();
$('#result').html(value)
});
Hope it will be work :)
I am trying to post a modal form to a table using php, jquery .ajax but it never works.. tried debugging using firebug and i don't see any errors. i tested the form by using form action="notes_functions.php" and it worked fine.
Profile.php
<div class="modal-body">
<form class="noteform" id="notesmodal" method="post">
<fieldset>
<label>Title</label>
<input type="text" class="form-control" name="note_title" placeholder="Enter a title for your note">
<label>Note</label>
<textarea rows="4" cols="50" class="form-control" name="note_content" placeholder="note"></textarea>
<label>Note type</label>
<div class="panel-body">
<input type="tagsinput" id="teetete" class="tagsinput" value="" />
</div>
<label for="exampleInputFile">Attach a document</label>
<input type="file" id="exampleInputFile3">
<p class="help-block">PDF, DOCX and image files are supported.</p>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
<input type="label" name="note_account" value="<?php echo $acctname ?>"/>
</label>
</div>
<input type="hidden" name="note_creator" value="<?php echo $_SESSION['username'];?>"/>
</fieldset>
<button class="btn btn-default" id="submitnote" >ADD</button>
</form>
</div>
this is my js code
$(function(){
$("button#submitnote").click(function(){
$.ajax ({
type:"POST",
url:"notes_functions.php",
data: $('form.noteform').serialize(),
success: function(msg){
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function(){
alert("failure");
}
});
});
});
notes_functions.php
<?php
include_once 'dbconnect.php';
if (isset($_POST['note_title'])) {
$notetitle = strip_tags($_POST['note_title']);
$noteContent = strip_tags($_POST['note_content']);
$noteAccount = strip_tags($_POST['note_account']);
$noteCreator = strip_tags($_POST['note_creator']);
mysql_query("INSERT INTO account_notes (note_title, note_contents, note_account, note_creator)
VALUES ('$notetitle','$noteContent', '$noteAccount', '$noteCreator') ");
echo "Name = ".$notetitle;
echo $noteCreator;
}
?>
You should really be using .submit() rather than click (for submit-by-enter, etc.) and returning false to block conventional submits. You also need to make sure the code to bind the events runs after the form element is created. The easiest way to do this is to put it in the document ready handler.
jQuery(document).ready(function ($) {
$("#notesmodal").submit(function () {
$.ajax({
type: "POST",
url: "notes_functions.php",
data: $('form.noteform').serialize(),
success: function (msg) {
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function () {
alert("failure");
}
});
return false;
});
});
And change the ADD button to be:
<input type="submit" name="submit" class="btn btn-default" id="submitnote" value="ADD" />
I'm just learning php and js and trying for a day to make this thing work...
Here's how it's supposed to be: after filling form you push button "Show modal" it shows modal window with input field. After submitting this form all the data from the form + phone from modal are send via email. When all input fields are simply in a form it all works fine. But when I show phone field in a modal it fails...
Here's the form
<div class="form">
<form class="form-deal form-deal1" id="form-order-3" name="special-price">
<input type="text" class="sqmeters" name="sqmeters" value="" />
<label>Select option:</label>
<div class="selectWrap">
<select class="formSelect" name="filmtype">
<option>Option one</option>
<option>ТOption two</option>
</select>
</div>
<label>Radio buttons</label>
<input type="radio" id="rb1" class="radio" name="regulator" value="r1"><label class="radioLabel" for="rb1">r1</label>
<input type="radio" id="rb2" class="radio" name="regulator" value="rb2"><label class="radioLabel" for="rb2">rb2</label>
<input type="radio" id="rb3" class="radio" name="regulator" value="rb3"><label class="radioLabel" for="rb3">rb3</label>
<label>Options:</label>
<div class="selectWrap">
<select class="formSelect" name="mount">
<option>opt1</option>
<option>opt2</option>
<option>opt3</option>
</select>
</div>
<div class="modal">
<input class="step2 hidden" type="text" name="phone" maxlength="14" placeholder="Phone:" required="" />
<button type="submit" class="btn step2 hidden">Submit form</button>
<span class="message"></span>
</div>
</form>
<a class="btn topcalcbtn">Show modal</a>
And the JS
$("form").submit(function(e){
e.preventDefault();
});
$(document).on('click', '.form-deal .btn', function () {
if ($(this).parent('form').find('input[name="phone"]').val().length>0 &&
$(this).parent('form').find('input[name="sqmeters"]').val().length>0)
AjaxFormRequest($(this).parent('form').attr('id'),'../mail_order.php');
});
function AjaxFormRequest(form_id, url) {
jQuery.ajax({
url: url,
type: "POST",
dataType: "html",
data: jQuery("#" + form_id).serialize(),
success: function (response) {
$('#'+form_id+' .message').text("Thanks!");
$('#'+form_id+' *:not(".message")').fadeOut(500);
$('.overlay, .popup').delay(2000).fadeOut(500);
$(window).attr('location','http://lp.tsk-energy.ru/thanx.html');
},
error: function (response) {
$('#'+form_id+' .message').text("Error");
}
});
}
Any help would be appreciated. Thanks in advance!
I am trying to post a modal form to a table using php, jquery .ajax but it never works.. tried debugging using firebug and i don't see any errors. i tested the form by using form action="notes_functions.php" and it worked fine.
Profile.php
<div class="modal-body">
<form class="noteform" id="notesmodal" method="post">
<fieldset>
<label>Title</label>
<input type="text" class="form-control" name="note_title" placeholder="Enter a title for your note">
<label>Note</label>
<textarea rows="4" cols="50" class="form-control" name="note_content" placeholder="note"></textarea>
<label>Note type</label>
<div class="panel-body">
<input type="tagsinput" id="teetete" class="tagsinput" value="" />
</div>
<label for="exampleInputFile">Attach a document</label>
<input type="file" id="exampleInputFile3">
<p class="help-block">PDF, DOCX and image files are supported.</p>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
<input type="label" name="note_account" value="<?php echo $acctname ?>"/>
</label>
</div>
<input type="hidden" name="note_creator" value="<?php echo $_SESSION['username'];?>"/>
</fieldset>
<button class="btn btn-default" id="submitnote" >ADD</button>
</form>
</div>
this is my js code
$(function(){
$("button#submitnote").click(function(){
$.ajax ({
type:"POST",
url:"notes_functions.php",
data: $('form.noteform').serialize(),
success: function(msg){
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function(){
alert("failure");
}
});
});
});
notes_functions.php
<?php
include_once 'dbconnect.php';
if (isset($_POST['note_title'])) {
$notetitle = strip_tags($_POST['note_title']);
$noteContent = strip_tags($_POST['note_content']);
$noteAccount = strip_tags($_POST['note_account']);
$noteCreator = strip_tags($_POST['note_creator']);
mysql_query("INSERT INTO account_notes (note_title, note_contents, note_account, note_creator)
VALUES ('$notetitle','$noteContent', '$noteAccount', '$noteCreator') ");
echo "Name = ".$notetitle;
echo $noteCreator;
}
?>
You should really be using .submit() rather than click (for submit-by-enter, etc.) and returning false to block conventional submits. You also need to make sure the code to bind the events runs after the form element is created. The easiest way to do this is to put it in the document ready handler.
jQuery(document).ready(function ($) {
$("#notesmodal").submit(function () {
$.ajax({
type: "POST",
url: "notes_functions.php",
data: $('form.noteform').serialize(),
success: function (msg) {
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function () {
alert("failure");
}
});
return false;
});
});
And change the ADD button to be:
<input type="submit" name="submit" class="btn btn-default" id="submitnote" value="ADD" />
I am still learning PHP, so bear with me. I am trying to create a sign up form that includes 2 DIVs. Once you click submit on the first div (personal info), it slides away and the second div (billing info) slides up using jQuery.
My problem is... I need some help figuring out how to determine if the submit function came from the first div or the second. If it's the first div, the slide happens. If it's the second div, the form is submitted.
HTML with Form
<div id="container">
<!-- #first_step -->
<div id="first_step">
<h1>SIGN UP FOR 17 FIT CLUB</h1>
<form class="signup" action="post/signup" method="post">
<input type="hidden" name="step" value="user" />
<div class="form">
<input type="text" name="email" id="email_signup" value="" />
<label for="email">Your email address. We send important administration notices to this address. </label>
<input type="text" name="confirmemail" id="cemail_signup" value="" />
<label for="username">Please re-type your email to verify it is correct.</label>
<input type="text" name="firstname" id="firstname_signup" value="" />
<label for="firstname">Your First Name. </label>
<input type="text" name="lastname" id="lastname_signup" value="" />
<label for="lastname">Your Last Name. </label>
<input type="text" name="username" id="username_signup" value="" />
<label for="username">At least 6 characters. Uppercase letters, lowercase letters and numbers only.</label>
<input type="password" name="password" id="password_signup" value="" />
<label for="password">At least 6 characters. Use a mix of upper and lowercase for a strong password.</label>
<input type="password" name="cpassword" id="cpassword_signup" value="" />
<label for="cpassword">If your passwords aren’t equal, you won’t be able to continue with signup.</label>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="submit" type="submit" name="submit_first" id="submit_first" value="submit"/>
</form>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<!-- #second_step -->
<div id="second_step">
<h1>SIGN UP FOR 17 FIT CLUB</h1>
<form class="signup" action="post/signup" method="post">
<input type="hidden" name="step" value="user" />
<div class="form">
<input type="text" name="nameoncard" id="nameoncard_signup" value="" />
<label for="email">Enter the name on the credit or debit card used for billing. </label>
<select name="cardtype" id="cardtype_signup" >
<option name="visa">Visa</option>
<option name="mastercard">Mastercard</option>
<option name="amex">American Express</option>
<option name="discover">Discover</option>
</select>
<label for="cardtype">What type of card are you using?</label>
<input type="text" name="cardnumber" id="cardnumber_signup" value="" />
<label for="cardnumber">Enter the card number.</label>
<div id="exp_date_signup">
<select name="exp_month" id="exp_month_signup" >
<option name="01">01</option>
<option name="02">02</option>
<option name="03">03</option>
<option name="04">04</option>
</select>
<select name="exp_year" id="exp_year_signup" >
<option name="12">12</option>
<option name="13">13</option>
<option name="14">14</option>
<option name="15">15</option>
</select>
</div>
<label for="exp_year">Enter the expiration date on the card.</label>
<input type="text" name="CVV2" id="cvv2_signup" value="" />
<label for="CVV2">Enter the 3 or 4 digit CVV2 number.</label>
<select name="country" id="country_signup">
<option value=" " selected>(please select a country)</option>
<option value="AF">Afghanistan</option>
<option value="ZM">...More options...</option>
<option value="ZW">Zimbabwe</option>
</select>
<label for="country">Enter the country for your billing address.</label>
<input type="text" name="billingaddress" id="billingaddress_signup" value="" />
<label for="bilingaddress">Enter the street name and number for the credit or debit card billing address.</label>
<input type="text" name="billingcity" id="billingcity_signup" value="" />
<label for="billingcity">Enter the city for you billing address.</label>
<select name="billingstate" id="billingstate_signup">
<option value="AL">Alabama</option>
<option value="AK">...More options...</option>
<option value="WY">Wyoming</option>
</select>
<label for="billingstate">Choose the state for your billing address.</label>
<input type="text" name="billingpostalcode" id="billingpostalcode_signup" value="" />
<label for="cpassword">Enter the postal code for your billing address.</label>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="send submit" type="submit" name="submit_second" id="submit_second" value="submit" />
</form>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
</div>
Javascript (I put "???" in the area I assume I need help)
<script type="text/javascript">
$(function(){
$('form.signup').submit(function(event){
event.preventDefault();
uri = $(this).attr('action');
data = $(this).queryString();
$.get(uri, data, function(response){
if(response.status == 0){
alert(response.message);
}
else if(???){
//show next step
$('#first_step').slideUp();
$('#second_step').slideDown();
}
else {
// redirect to internal home page
window.location = '<?=PROTOCOL?>//<?=DOMAIN?>/home';
}
}, 'json');
});
$('form.signup input').focus(function(event){
if(!$(this).hasClass('clicked')){
$(this)
.val('')
.addClass('clicked');
}
});
});
</script>
Any help would be greatly appreciated! I am sure this has a simple solution, but I haven't been able to crack it. n00b!
UPDATE:
ANSWER LISTED BELOW
What I would recommend is to combine both of the forms into only one form; split up the two "forms" with div tags with two separate buttons and then have jquery like this
//First Button Click Handler
$.("form div#first input.submit").click(function(){
$("div.first").slideUp('fast',function(){
$("div.second").slideDown('fast');
});
});
//Second Button Click Handler
$.("form div#second input.submit").click(function(){
var data = $('form').serialize();
var url = "whatever";
$.get(url,data,function(response){
//Handle Response
});
});
The trick is to disable the form's normal submit triggers and then handle them directly using specific click handlers
Something like this in the html will stop your form submitting by default
<form onsubmit="return false;">
<input type="password"/>
<input type="submit"/>
</form>
Have a next button that calls the jQuery function and the submit button at the bottom of the billing info. Ex:
function(slide){
$('#first_step').slideUp();
$('#second_step').slideDown();
}
Here is the answer to my question. I figured it out with the assistance of a person in RL. Both submit inputs have their own value, "user" and "billing" respectively.
<script type="text/javascript">
$(function(){
$('form.signup').submit(function(event){
event.preventDefault();
uri = $(this).attr('action');
data = $(this).queryString();
step = $(this).find('input[name=step]').val(); // <--update
if(response.status == 0){
alert(response.message);
}
else{ // <--update
if(step == 'user'){ // <--update
//show next slide // <--update
$('#first_step').slideUp(); // <--update
$('#second_step').slideDown(); // <--update
} // <--update
else if(step == 'billing'){ // <--update
//redirect to internal home page // <--update
window.location = '<?=PROTOCOL?>//<?=DOMAIN?>/home'; // <--update
} // <--update
}
}, 'json');
});
$('form.signup input').focus(function(event){
if(!$(this).hasClass('clicked')){
$(this)
.val('')
.addClass('clicked');
}
});
});
</script>