I am using $_SESSION to dynamically create forms for my web store. These forms hold the custom info for the product that the customer wants. This is the layout:
Page1
Customer fills out form that looks something like this:
<form action="page2" method="post">
<input type="text" name="size">
<input type="text" name="color">
<input type="submit" name="submit" value="Review Order">
</form>
Page2
Customer reviews order details and has the option of adding more products. Customer goes back to page1 to order another one. All of the customer's orders will show on page2 in their respective form.
Looks like this:
Size: 1
Color: blue
Click Here To Checkout
Size: 2
Color:green
Click Here To Checkout
Size:3
color:red
Click Here To Checkout
What I want is one button that will add ALL orders to the PayPal cart. Sure they can add every order individually by clicking on Click Here To Checkout, but then they will have to go through a big loop to add multiple products.
I want the customer to be able to add as many products as possible and then click one button that adds all of the orders to the shopping cart.
This is what I tried but it obviously didn't work:
<script>
$(document).ready(function(){
$('#clickAll').on('click', function() {
$('input[type="submit"]').trigger('click');
});
});
</script>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<button id="clickAll">Submit All</button>
Here is the php script that generates the dynamic forms using $_SESSION:
<?php
if(isset($_POST['submit'])) :
$test = array(
'size' => $_POST['size'],
'color' => $_POST['color'],
'submit' => $_POST['submit']
);
$_SESSION['testing'][] = $test;
endif;
if(isset($_SESSION['testing'])) :
foreach($_SESSION['testing'] as $sav) {
?>
<form action="paypal.com/..." method="post">
<input type="text" name="size" value="<?php echo $sav['size']; ?>">
<input type="text" name="color" value="<?php echo $sav['color']; ?>">
<input type="submit" name="submit" value="Click Here to Checkout">
</form>
<?php } endif; ?>
So the question is, how can I submit all of the forms with ONE button?
Have you tried to do it with $.ajax? You can add an foreach, or call another form on the Onsucces function. Another approach is changing all to one form with an array that points to the right "abstract" form:
<form action="" method="post">
<input type="text" name="name[]">
<input type="text" name="example[]">
<input type="text" name="name[]">
<input type="text" name="example[]">
<input type="text" name="name[]">
<input type="text" name="example[]">
<button id="clickAll">Submit All</button>
</form>
And in php:
foreach ($_POST['name'] as $key => $value) {
$_POST['name'][$key]; // make something with it
$_POST['example'][$key]; // it will get the same index $key
}
Here is a working jsFiddle: http://jsfiddle.net/SqF6Z/3/
Basically, add a class to each form and trigger() a submit on that class. Like so:
HTML (example only):
<form action="http://www.google.com" method="get" class="myForms" id="1stform">
<input type="text" value="1st Form" name="q1" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="2ndform">
<input type="text" value="2nd Form" name="q2" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="3rdform">
<input type="text" value="3rd Form" name="q3" />
</form>
<input type="button" id="clickMe" value="Submit ALL" />
jQuery:
$('.myForms').submit(function () {
console.log("");
return true;
})
$("#clickMe").click(function () {
$(".myForms").trigger('submit'); // should show 3 alerts (one for each form submission)
});
FWIW, I do this by creating an iframe, making that the target for the second form then submit both like this
//create the iframe
$('<iframe id="phantom" name="phantom">').appendTo('#yourContainer');
and create the dual submit like this:
function dualSubmit() {
document.secondForm.target = 'phantom';
document.secondForm.submit();
document.firstForm.submit();
}
works!
first create loop get all forms id and send them to ajax.
<script name="ajax fonksiyonları" type="text/javascript">
function validate(form){
//get form id
var formID = form.id;
var formDetails = $('#'+formID);
$.ajax({
type: "POST",
url: 'ajax.php',
data: formDetails.serialize(),
success: function (data) {
// log result
console.log(data);
//for closing popup
location.reload();
window.close()
},
error: function(jqXHR, text, error){
// Displaying if there are any errors
console.log(error);
}
});
return false;
}
//this function will create loop for all forms in page
function submitAll(){
for(var i=0, n=document.forms.length; i<n; i++){
validate(document.forms[i]);
}
}
create button for submit in order
<a class="btn" id="btn" onclick="submitAll();" href="">Save & Close</a>
then stop ajax call after success.also dont forget to log to console.
this code works in popup and closing popup after all ajax completed.
Related
I am using $_SESSION to dynamically create forms for my web store. These forms hold the custom info for the product that the customer wants. This is the layout:
Page1
Customer fills out form that looks something like this:
<form action="page2" method="post">
<input type="text" name="size">
<input type="text" name="color">
<input type="submit" name="submit" value="Review Order">
</form>
Page2
Customer reviews order details and has the option of adding more products. Customer goes back to page1 to order another one. All of the customer's orders will show on page2 in their respective form.
Looks like this:
Size: 1
Color: blue
Click Here To Checkout
Size: 2
Color:green
Click Here To Checkout
Size:3
color:red
Click Here To Checkout
What I want is one button that will add ALL orders to the PayPal cart. Sure they can add every order individually by clicking on Click Here To Checkout, but then they will have to go through a big loop to add multiple products.
I want the customer to be able to add as many products as possible and then click one button that adds all of the orders to the shopping cart.
This is what I tried but it obviously didn't work:
<script>
$(document).ready(function(){
$('#clickAll').on('click', function() {
$('input[type="submit"]').trigger('click');
});
});
</script>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<form action="" method="post">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<button id="clickAll">Submit All</button>
Here is the php script that generates the dynamic forms using $_SESSION:
<?php
if(isset($_POST['submit'])) :
$test = array(
'size' => $_POST['size'],
'color' => $_POST['color'],
'submit' => $_POST['submit']
);
$_SESSION['testing'][] = $test;
endif;
if(isset($_SESSION['testing'])) :
foreach($_SESSION['testing'] as $sav) {
?>
<form action="paypal.com/..." method="post">
<input type="text" name="size" value="<?php echo $sav['size']; ?>">
<input type="text" name="color" value="<?php echo $sav['color']; ?>">
<input type="submit" name="submit" value="Click Here to Checkout">
</form>
<?php } endif; ?>
So the question is, how can I submit all of the forms with ONE button?
Have you tried to do it with $.ajax? You can add an foreach, or call another form on the Onsucces function. Another approach is changing all to one form with an array that points to the right "abstract" form:
<form action="" method="post">
<input type="text" name="name[]">
<input type="text" name="example[]">
<input type="text" name="name[]">
<input type="text" name="example[]">
<input type="text" name="name[]">
<input type="text" name="example[]">
<button id="clickAll">Submit All</button>
</form>
And in php:
foreach ($_POST['name'] as $key => $value) {
$_POST['name'][$key]; // make something with it
$_POST['example'][$key]; // it will get the same index $key
}
Here is a working jsFiddle: http://jsfiddle.net/SqF6Z/3/
Basically, add a class to each form and trigger() a submit on that class. Like so:
HTML (example only):
<form action="http://www.google.com" method="get" class="myForms" id="1stform">
<input type="text" value="1st Form" name="q1" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="2ndform">
<input type="text" value="2nd Form" name="q2" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="3rdform">
<input type="text" value="3rd Form" name="q3" />
</form>
<input type="button" id="clickMe" value="Submit ALL" />
jQuery:
$('.myForms').submit(function () {
console.log("");
return true;
})
$("#clickMe").click(function () {
$(".myForms").trigger('submit'); // should show 3 alerts (one for each form submission)
});
FWIW, I do this by creating an iframe, making that the target for the second form then submit both like this
//create the iframe
$('<iframe id="phantom" name="phantom">').appendTo('#yourContainer');
and create the dual submit like this:
function dualSubmit() {
document.secondForm.target = 'phantom';
document.secondForm.submit();
document.firstForm.submit();
}
works!
first create loop get all forms id and send them to ajax.
<script name="ajax fonksiyonları" type="text/javascript">
function validate(form){
//get form id
var formID = form.id;
var formDetails = $('#'+formID);
$.ajax({
type: "POST",
url: 'ajax.php',
data: formDetails.serialize(),
success: function (data) {
// log result
console.log(data);
//for closing popup
location.reload();
window.close()
},
error: function(jqXHR, text, error){
// Displaying if there are any errors
console.log(error);
}
});
return false;
}
//this function will create loop for all forms in page
function submitAll(){
for(var i=0, n=document.forms.length; i<n; i++){
validate(document.forms[i]);
}
}
create button for submit in order
<a class="btn" id="btn" onclick="submitAll();" href="">Save & Close</a>
then stop ajax call after success.also dont forget to log to console.
this code works in popup and closing popup after all ajax completed.
I have one form with a few submit buttons. I want to submit the form via POST to itself to process the filled out form fields... does the jquery override the submit?
$('#myButton').click('myAction') <!-- not actual code, just for the idea -->
<input type="button" type="submit" id="myButton" value="do something">
you can use something like this:
html form:
<form id="myform" method="post" name="form" action="">
<input type="text" name="name" value="">
<input type="text" name="other" value="">
<input type="submit" type="submit" name="myButton" value="do something">
</form>
js:
$('#myform').on('submit',function(e){
e.preventDefault();
var addnew='&addnew=1234';//additional data
var _data = $('#myform').serialize();
_data = _data+addnew;
console.log(_data);
});
sample jsfiddle
I have a profile edit form below in which I am asking the user to select multiple cities (I left 4, but on my page I am looking to add around 29,000) from a select2 dropdown menu using the following code, but the selected values do not save when I press the submit button and therefore when the page reloads there are no selected values.
<form action="/edit-my-profile/?action=edit&job_id=5792" method="post" id="submit-job-form" class="job-manager-form" enctype="multipart/form-data">
<fieldset class="fieldset-html">
<label for="html">HTML</label>
<div class="field ">
<select name='job_listing_region[]' id='regions' style='width:300px' multiple>
<option>Boca Raton,FL</option>
<option>New York City, NY</option>
<option>Los Angeles, CA</option>
<option>Boone, NC</option>
</select>
</div>
</fieldset>
<p>
<input type="hidden" name="job_manager_form" value="edit-job" />
<input type="hidden" name="job_id" value="5792" />
<input type="hidden" name="step" value="0" />
<input type="submit" name="submit_job" class="button" value="Save changes" />
</p>
</form>
and the following jquery script
<script>
jQuery(function($) {
$(document).ready(function() {
$("#regions").select2({
});
});
});
</script>
Is there a way I can have the selection box save the selected values?
<?php
echo $_POST['textvalue'];
echo $_post['radiovalue'];
?>
<div id="hidethis">
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</div>
http://jsfiddle.net/Bjk89/2/ here is it with the jQuery.
What i try to do is to hide the <div id="hidethis"> when it's clicking submit.
I know i can make another page where i can recieve the values without the <form> section, but i want to put both in one page, make the <div id="hidethis"> hidden after submit.
So i'll be able to get echo $_POST['textvalue']; and echo $_post['radiovalue']; as results
RESULT MUST BE LIKE
A Text // This is the value you input into Tekst Value
autogivevalue // This is the value from the radio button
----- INVISIBLE -----
<form is hidden because we set it in jQuery so>
</form>
Try this. No need to use jQuery here.
<?php
if($_POST) {
echo $_POST['textvalue'];
echo $_post['radiovalue'];
} else {
?>
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</form>
<?php
}
?>
Try adding '#' in your jquery code. Your version does not have # next to submit. Also your form is missing a closing tag here and in your JSFiddle code.
Try this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#submit').click(function () {
$('form').submit();
$('#hidethis').hide();
});
});
</script>
<form method='post' id="hidethis" name='form'>
<input type="text" name="textvalue">
<input type="radio" name="radiovalue" value="1">
<input type="button" id="submit" name="submit" value="submit">
</form>
I have two different forms which are being included in a php file. Their visibility is based on a onClick JS toggle function. The toggle works great. However if I was to fill only one of the forms out and click its respective submit button, I get sent back to the same page rather than the action.php file that i have specifed with this in the broswer:
http://localhost/?user_temp=f&pass_temp1=f&pass_temp2=ff&email_temp=f&answer1=3&submitbtn=Signup+Now
And this Javascript error: "TypeError undefined document.hform.sSecureUser
Both Forms also have their own javascripts to MD5 some data and sSecureUser comes from the Signup Form.
Interestingly, if I was to remove one of the inlcude forms leaving only the Submit form lets say, it would work. It seems that these forms' javascript is clashing with one another :/ ...
I tried this but it didnt work for me since each one of my forms is using java script. PLEASE HELP AND THANKS IN ADVANCE!!!... Let me know if you would like to see any of my forms, javascript, or php files...
Toggle Code:
<div>
Login
<div id="login-div" style="display:none">
<?php include($_SERVER['DOCUMENT_ROOT'].'/forms/login-form.php');?>
</div>
Sign up
<div id="signup-div" style="display:none">
<?php include($_SERVER['DOCUMENT_ROOT'].'/forms/signup-form.php'); ?>
</div>
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block'){
e.style.display = 'none';
}
else{
e.style.display = 'block';
}
}
//-->
</script>
</div>
Login Form:
<div id="hasJavaScript1" style="display:none">
<form name="login">
Username:
<input type="text" name="user_temp" size=32 maxlength=32><br>
Password:
<input type="password" name="pass_temp" size=32 maxlength=32><br>
<input onClick="passResponse(); return false;" type="submit" name="submitbtn" value="Login now">
</form>
<form action="/action/login-action.php" METHOD="POST" name="hform">
<input type="hidden" name="secureuser">
<input type="hidden" name="securepass">
</form>
</div>
<script language="javascript" src="/js/md5.js"></script>
<script language="javascript">
<!--
document.getElementById('hasJavaScript1').style.display = 'block';
function passResponse() {
document.hform.secureuser.value = MD5(document.login.user_temp.value);
document.hform.securepass.value = MD5(document.login.pass_temp.value);
document.login.pass_temp.value = "";
document.hform.submit();
}
// -->
</script>
SignUP Form:
<?php include ($_SERVER['DOCUMENT_ROOT'].'/functions/functions.php');?>
<div id="hasJavaScript" style="display:none">
<form name="signup">
<label>Username</label> <input type="text" name="user_temp" size=32 maxlength=32><span>alphanumeric, no spaces</span><br>
<label>Type password</label> <input type="password" name="pass_temp1" size=32 maxlength=32><span>alphanumeric, 8-12 long</span><br>
<label>Retype password</label> <input type="password" name="pass_temp2" size=32 maxlength=32><br>
<label>Email</label> <input type="text" name="email_temp" size=32 maxlength=32><br>
<label> What is: </label><?php $captchaArray = myCaptcha(); echo $captchaArray['equation'];?><br>
<label>Answer</label><input type="text" name="answer1">
<input onClick="passResponse1(); return false;" type="submit" name="submitbtn" value="Signup Now">
</form>
<form action="/action/signup-action.php" METHOD="POST" name="signup-hform">
<input type="hidden" name="sSecureUser">
<input type="hidden" name="sSecurePass1">
<input type="hidden" name="sSecurePass2">
<input type="hidden" name="secureEmail">
<input type="hidden" name="answer2">
<input type="hidden" name="checker" value=".<?php echo $captchaArray['answer'];?> .">
</form>
</div>
<script language="javascript" src="/js/md5.js"></script>
<script language="javascript">
<!--
document.getElementById('hasJavaScript').style.display = 'block';
function passResponse1() {
document.signup-hform.sSecureUser.value = MD5(document.signup.user_temp.value);
document.signup-hform.sSecurePass1.value = MD5(document.signup.pass_temp1.value);
document.signup.pass_temp1.value = "";
document.signup-hform.sSecurePass2.value = MD5(document.signup.pass_temp2.value);
document.signup.pass_temp2.value = "";
document.signup-hform.secureEmail.value = MD5(document.signup.email_temp.value);
document.signup-hform.answer2.value = document.signup.answer1.value;
document.signup.answer1.value = "";
document.signup-hform.submit();
}
// -->
</script>
One problem I see is that you do not specify any value attributes or values in your hidden fields:
<input type="hidden" name="sSecureUser">
<input type="hidden" name="sSecurePass1">
<input type="hidden" name="sSecurePass2">
<input type="hidden" name="secureEmail">
<input type="hidden" name="answer2">
But then in your JS, you're trying to access the value attribute.
document.hform.sSecureUser.value = ...
Try adding values to those. I would also give your hidden forms ids and use the document.getElementById() syntax instead of the document.hform.sSecureUser syntax.
As far as your form submits, you are suppressing the action of the first form, trying to fill in values for the hidden fields in the second form and then submitting that. Both of your scripts use the same name for the forms "hform". They need to be unique. I would also give them a unique id.
Another possible issue is this line:
<input type="hidden" name="checker" value=".<?php echo $captchaArray['answer'];?> .">
You're using the . for string concatenation, but it's not in an echo/print statement. I think it's supposed to be:
<input type="hidden" name="checker" value="<?php echo $captchaArray['answer'];?>">