...

View Full Version : Form submit



nick2price
04-04-2013, 01:23 AM
Hi guys. will try my best to explain my problem, hopefully you can help :)

I have a JQuery Mobile app which is like a 3 step form. The header/footer remain the same throughout the app, and I just show/hide the center content as I need too.

In the first form, the user enters the data, and on submit, I currently do this


$('#form2').submit(function () {
// Hide the Main content
hideMain();

// Submit the form
$.post("forms/requestProcessor.php", form1Var.serialize(), function (data) {
confirmationVar.text(data);
hideContentTransition();
showConfirmation();
});
return false;
});

Really, this is doing nothing at the moment, infact I have the exact same code for my second forms submit.

What I really need to do though is when the second form is submit, I get the data from form 1 and form 2, and I send that to an email address. At the moment, my forms are pretty much in the form of

<form id="form2">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<label for="name"> Name </label>
<input name="name_g" id="name" value="" type="text">
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<label for="phone"> Phone </label>
<input name="phone_g" id="phone" value="" type="text">
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<label for="email"> Email </label>
<input name="email_g" id="email" value="" type="text">
</fieldset>
</div>
<div data-role="fieldcontain">
<input id="submit-btn2" type="submit" value="Submit" data-inline="true"/>
</div>
</form>

So if I remove the Ajax code from my first form, but keep it in my second, how can I retrieve the data from both forms within requestProcessor.php.

Any information appreciated, and feel free to move if this is more php than ajax.

Many thanks

WolfShade
04-04-2013, 01:36 PM
You could set the information from the first form into hidden fields in the second form and submit all of the data on one submit.

nick2price
04-04-2013, 04:36 PM
Ok, thats understandable. So I have a form like in my original post. Then in my js, I listen for the form submit by doing

$('#form1').submit(function () {

return false;
});

But then how would I get the values to form 2? Just out of interest, if I am doing this all in one page (I show/hide the main content) do I need to pass the data to form 2. Wouldnt it be immediately available?

WolfShade
04-04-2013, 05:13 PM
Not return false - that would prevent the form from submitting.. keep submitting as you do with AJAX, but have the first form submit to the second form, and near the top of the second form code your hidden fields with the same names and populate them from the PHP POST. Then the second form will submit to a processing page that processes form2 data which includes form1 data.

DanInMa
04-04-2013, 06:50 PM
how about this? never tried it, but in theory, it should work



$('#form2').submit(function () {
// Hide the Main content
hideMain();

// Submit the form
$.post("forms/requestProcessor.php", $("#form1,#form2").serialize(), function (data) {
confirmationVar.text(data);
hideContentTransition();
showConfirmation();
});
return false;
});

nick2price
04-04-2013, 07:05 PM
Thats more like what I was thinking, just send both forms over. How can I check if this works? Whats the way to get this data printed out in the php file? I am not sure if I just do something like

$myVar = $_POST("#form1");

WolfShade
04-04-2013, 07:30 PM
I misunderstood the original question. I did not realize that both forms were on the same page.

rnd me
04-04-2013, 08:39 PM
in most browsers, you can point elements to different forms using the form attribute, which is now read/write:


$("#form1 [name]").map(function(b,a){
a.form="form2";
});

form2.submit();

nick2price
04-04-2013, 08:43 PM
This is for a mobile site so I better use a safe option. Once I pass the data from both forms to php, as seens in one of the replies, how can I go about retrieving the values in php? From my understanding, it will be a serialized string, so I would need to somehow unserialize it?

rnd me
04-04-2013, 08:45 PM
This is for a mobile site so I better use a safe option. Once I pass the data from both forms to php, as seens in one of the replies, how can I go about retrieving the values in php? From my understanding, it will be a serialized string, so I would need to somehow unserialize it?

each input is a property of $_POST.

so, <input name=lastName /> would be available as $_POST['lastName'].

you can likely iterate the POST or FORM objects in php to get all the keys, read the php manual page for POST for cut-and-paste code.

something like

$kv = array();
foreach ($_POST as $key => $value) {
$kv[] = "$key=$value";
}

cssbutton
04-10-2013, 03:17 PM
each input is a property of $_POST.

so, <input name=lastName /> would be available as $_POST['lastName'].

you can likely iterate the POST or FORM objects in php to get all the keys, read the php manual page for POST for cut-and-paste code.

something like

$kv = array();
foreach ($_POST as $key => $value) {
$kv[] = "$key=$value";
}

i will advice you use jquery to hide the first form on user submit and display the second form which you will pass to you php script and use the code above to collect the variables



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum