1

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
      }
 ?>
1
  • It should be $type = $_POST["type"] Commented Oct 22, 2013 at 10:18

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.