Reputation: 2079
I have a js
switch declaration that checks if a specific string is present in the submitted text.
var textA= //regex
var textB= //regex
switch(true) {
case textA.test(input):
// CASE A
$ajax ({
type:"post",
url:"process.php",
data: {input:input,age:age,city:city,type:type},
success: function(html){alert(html);} });
break;
case textB.test(input):
// CASE B
$ajax ({
type:"post",
url:"process.php",
data: {input:input,width:width,height:height,type:type},
success: function(html){alert(html);} });
break;
case ...
}
normally, i would create dedicated php
file to handle the POST data for each of $ajax
.
but how can i handle multiple $ajax
POST in a single php
.
i included a unique identifier type:
for each ajax data, this will be my reference as to what ajax is being received by my PHP
but im not sure how to properly code PHP to handle the submitted $_POST type.
<?php
//get post type from submitted AJAX
$type = $_POST;
switch($type) {
case 0:
$type= "caseA"
//some code here
case 1:
$type= "caseB"
// some code here
}
?>
Upvotes: 1
Views: 1504
Reputation: 1140
You can do it like this:
switch(true) {
case textA.test(input):
// CASE A
$ajax ({
type:"post",
url:"process.php",
data: {input:input,age:age,city:city,type:"typeA"},
success: function(html){alert(html);} });
break;
case textB.test(input):
// CASE B
$ajax ({
type:"post",
url:"process.php",
data: {input:input,width:width,height:height,type:"typeB"},
success: function(html){alert(html);} });
break;
case ...
}
And then in PHP:
<?php
switch($_POST['type']) { // or $_REQUEST['type']
case 'typeA':
// Type A handling code here
break;
case 'typeB':
// Type B handling code here
break;
}
Upvotes: 1
Reputation: 4584
Send an action per case,
e.g.
$.ajax({
url: 'path/to/file.php',
type: 'POST / GET',
data: {action: 1, data:myObject}
});
Every case, send a different action, then in PHP check that with $_POST / $_GET['action']
so you can do a switch
statement
e.g.
switch($_POST / $_GET['action']) {
case 1:
//do something
break;
}
Upvotes: 2