How to post two parameters in php using ajax?

by cortez.connelly , in category: PHP General , 8 months ago

How to post two parameters in php using ajax?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 8 months ago

@cortez.connelly 

To post two parameters in PHP using AJAX, you can do the following:

  1. Create an AJAX request with the POST method and specify the URL of the PHP file that will handle the request.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var param1 = 'value1';
var param2 = 'value2';

$.ajax({
  type: 'POST',
  url: 'process.php',
  data: { param1: param1, param2: param2 },
  success: function(response) {
    console.log(response);
  }
});


  1. In your PHP file (process.php in this example), retrieve the posted parameters using the $_POST superglobal and process them accordingly.
1
2
3
4
5
6
7
<?php 
  $param1 = $_POST['param1'];
  $param2 = $_POST['param2'];

  // Process the parameters
  echo "Parameter 1: $param1, Parameter 2: $param2";
?>


By following these steps, you can post two parameters in PHP using AJAX.