AJAX HTML Javascript jQuery PHP Example MORE Videos New

How to send array to php script file using ajax


Here we using 2 file for send array data to php script file using ajax

  1. index.php
  2. save.php

index.php

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
$(document).on('click','#save',function(e) {
    	var vehicle = [];
		/*Initializing array with Checkbox checked values*/
        $("input[name='vehicle']:checked").each(function(){
            vehicle.push(this.value);
        });
    $.ajax({
         data:  {
			 vehicle:vehicle
		 },
         type: "post",
         url: "save.php",
         success: function(data){
              alert(data);
         }
	});
});
</script>
</head>
<body>
	<form action="" id="form-search">
	  	<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
		<input type="checkbox" name="vehicle" value="Car"> I have a car<br>
		<input type="checkbox" name="vehicle" value="Boat"> I have a boat<br>
	</form>
	<button id="save" name="save">Serialize form values</button>
</body>
</html>
  

save.php

<?php
    $vehicle=$_POST['vehicle'];
    print_r($vehicle);
?>