Reputation: 33
I have two submit buttons on one form. I am using serialize method (so it will not refresh the page) to send the form data to php page. If the two buttons send data to one php page, how can I tell which part of the php script to be processed depending on the button that was pressed? Similar questions I saw on this site did not help me. My code is shown below.
HTML
<form id='form_id' action='my_page.php' method='post'>
<input type='text' name='quest1' value='$question'>
<input type='text' name='test1_id' value='$test_idq'>
<input type='text' name='test_code1' value='$code'>
<input type='text' name='class_name1' value='$class_name' >
<input type='text' name='test_name1' value='$test_nameq'>
<button id='response' class='btn btn-danger'>Response</button>
<button id='subm' class='btn btn-success''><b>Save</b></button>
</form>
Javascript
//This script processes the button with the id=subm
$('body').on('click', '#subm', function(){
$.post($("#form_id").attr("action"), $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
$("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
return false;
$('#subm').click(function(){
})
});
//This script processes the button with the id=response
$('body').on('click', '#response', function(){
$.post($("#form_id").attr("action"), $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
$("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
return false;
$('#response').click(function(){
})
});
PHP
<?php
include("includes/db.php");
$questn1 = $_POST['quest1'];
$class1 = $_POST['class_name1'];
$test_id1 = $_POST['test1_id'];
$t_code1 = $_POST['test_code1'];
$t_name1 = $_POST['test_name1'];
//php code continues
}
?>
Upvotes: 1
Views: 79
Reputation: 424
You can do it in diferents way one is to make two php page scripts.
example:
save.php
<?php
include("includes/db.php");
$questn1 = $_POST['quest1'];
$class1 = $_POST['class_name1'];
$test_id1 = $_POST['test1_id'];
$t_code1 = $_POST['test_code1'];
$t_name1 = $_POST['test_name1'];
//php code continues
}
?>
response.php
<?php
include("includes/db.php");
$questn1 = $_POST['quest1'];
$class1 = $_POST['class_name1'];
$test_id1 = $_POST['test1_id'];
$t_code1 = $_POST['test_code1'];
$t_name1 = $_POST['test_name1'];
//php code continues
echo $response;
}
?>
Javascript
//This script processes the button with the id=subm
$('body').on('click', '#subm', function(){
$.post('save.php', $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
$("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
return false;
$('#subm').click(function(){
})
});
//This script processes the button with the id=response
$('body').on('click', '#response', function(){
$.post('response.php', $("#form_id").serialize(), function(info){ $("#msg").html(info); } );
$("html, body").animate({ scrollTop: 0 }, "slow");
});
$('body').on('submit', '#form_id', function(event){
return false;
$('#response').click(function(){
})
});
Upvotes: 0
Reputation: 13
You can give each input a different value:
<input type="submit" name="action" value="Update" />
<input type="submit" name="action" value="Delete" />
You can find the whole answer in this question: Two submit buttons in one form
Upvotes: 1
Reputation: 5041
Set the value of a hidden input based on what submit button is pressed then process the form based on that input.
HTML
<input type='hidden' id="action" name='action' value=''>
<button type="submit" id='response' value="response" class='btn btn-danger'>Response</button>
<button type="submit" id='subm' value="subm" class='btn btn-success''><b>Save</b></button>
Javascript
$('body').on('click', '[type="submit"]', function(){
$('#action').val($(this).val());
});
PHP
<?php
include("includes/db.php");
if($_POST['action'] == 'response' ){
// do something
}
else if($_POST['action'] == 'subm' ){
// do something else
}
Upvotes: 0
Reputation: 326
You can add a <input type='hidden' name='action' value='-'>
And then in your button click function set the value of this hidden input before serializing the form values.
Upvotes: 0