javascript - how to add $_POST['submit'] in ajax jquery or what is the best way to do this..? ← (PHP, JavaScript, JQuery, HTML)
index.php
<form id="myForm" action="test2.php" method="POST">
<input type="submit" value="Print 1" name="submit" id="submit">
<input type="submit" value="Print 2" name="submit2" id="submit2">
</form>
<div id="ack"></div>
here's my script
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="scripts/login_script.js"></script>
test2.php
<?php
if(isset($_POST['submit'])){
echo "1"; // i want to show this everytime i click submit (not working)
} // or what is the proper way to do this..
if(isset($_POST['submit2'])){
echo "2"; // i want to show this everytime i click submit2 (not working)
} // or what is the proper way to do this..
?>
login_script.js
$("#submit, #submit2").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(data) {
$("#ack").empty();
$("#ack").html(data);
});
$("#myForm").submit( function() {
return false;
});
});
if i remove the if(isset($_POST['submit'])).... its working... so what is the proper way to do this..? sorry im noob in ajax and jquery
Answer
Solution:
No, you have to realize that <input type="submit"
and <button> submit
is not included in .serializeArray()
, you'll have to just build the data object to be passed:
$("#submit, #submit2").click( function() {
var data = null;
if($(this).attr('id') == 'submit') {
values = {'submit': true};
} else {
values = {'submit2': true};
}
$.post( $("#myForm").attr("action"), values,
function(data) {
$("#ack").empty();
$("#ack").html(data);
});
});
Answer
Solution:
try with this:
$("#submit, #submit2").click( function() {
var formData = $(this).closest('form').serializeArray();
formData.push({ name: this.name, value: this.value });
$.post( $("#myForm").attr("action"),formData,
function(data) {
$("#ack").empty();
$("#ack").html(data);
});
$("#myForm").submit( function() {
return false;
});
});
Answer
Solution:
Yes I just try it and below code is working for me please check.
index.php
<form id="myForm" action="test2.php" method="POST">
<input type="submit" value="Print 1" name="submit" id="submit">
<input type="submit" value="Print 2" name="submit2" id="submit2">
</form>
<div id="ack"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$("#submit, #submit2").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(data) {
$("#ack").empty();
$("#ack").html(data);
});
$("#myForm").submit( function() {
return false;
});
});
</script>
test.php
<?php
if(isset($_POST['submit'])){
echo "1"; // i want to show this everytime i click submit (not working)
} // or what is the proper way to do this..
if(isset($_POST['submit2'])){
echo "2"; // i want to show this everytime i click submit2 (not working)
} // or what is the proper way to do this..
?>
Answer
Solution:
In the post it user name instead of the ID. Please try code below hope it will help you.
<form id="myForm" action="test2.php" method="POST">
<input type="submit" value="Print 1" name="submit" id="submit">
<input type="submit" value="Print 2" name="submit2" id="submit2">
</form>