You can use javascript to catch the submit form, or you can call the function you created. It does not have to be 3 forms or even a form element for the second case to work
HTML:
<div id="tab-1" class="ui-tabs-panel ui-tabs-hide"> <form autocomplete="off" method="post" enctype="multipart/form-data" id="postForm1" name="postForm1" class="postForm"> Name <input type="text" id="name" class="detail" name="name" value="" /> Age <input type="text" id="age" class="detail" name="age" value="" /> </form> </div>. <div id="tab-2" class="ui-tabs-panel ui-tabs-hide"> <form autocomplete="off" method="post" enctype="multipart/form-data" id="postForm2" name="postForm2" class="postForm"> Phone Number <input type="text" id="phone" class="detail" name="phone" value="" /> Email <input type="text" id="email" class="detail" name="email" value="" /> </form> </div> <div id="tab-3" class="ui-tabs-panel ui-tabs-hide"> <form autocomplete="off" method="post" enctype="multipart/form-data" id="postForm3" name="postForm3" class="postForm"> Job <input type="text" id="job" class="detail" name="job" value="" /> Hobby <input type="text" id="hobby" class="detail" name="hobby" value="" /> <input type="button" value="SAVE" onclick="saveThem()" /> </form> </div>
AJAX:
function saveThem() { var name = $.trim($("#name").val()); var age = $.trim($("#age").val()); var phone = $.trim($("#phone").val()); var email = $.trim($("#email").val()); var job = $.trim($("#job").val()); var hobby = $.trim($("#hobby").val()); var dataString = 'name='+name+'&age='+age+'&phone='+phone+'&email='+email+'&job='+job+'&hobby='+hobby; $.ajax({ type: "POST", url: 'http://www.yourdomain.com/something.php', data: dataString, dataType: "json", success: function(data) { if(data.response){ alert(data.message); } $("#inputResult").html(data); } }); }
something.php
//Get posted variables $name = (isset($_POST['name'])) ? strip_tags($_POST['name']) : NULL; $age = (isset($_POST['age'])) ? strip_tags($_POST['age']) : NULL; $phone = (isset($_POST['phone'])) ? strip_tags($_POST['phone']) : NULL; $email = (isset($_POST['email'])) ? strip_tags($_POST['email']) : NULL; $job = (isset($_POST['job'])) ? strip_tags($_POST['job']) : NULL; $hobby = (isset($_POST['hobby'])) ? strip_tags($_POST['hobby']) : NULL; //execute your query $sql = "INSERT INTO ..."; //Return results back to ajax $result = array( 'response' => 1, 'message' => 'We have a success!' ); echo json_encode($result);
So in conclusion, it's almost the same as using php, but in the middle there is some javascript.
Form : create an identifier for each element that you want to send to your server (php)
Js : catch values ββbased on item ids and sending them to php
Php : get values, clear them, query or something else and send the results back to js
EDIT: using jQuery serialize ()
$('form').submit(function() { $dataString = $(this).serialize();
source share