1

enter image description herei have store label values in array, pass this array using ajax to php. now i want to store each index value in different variables the store these value under one field of mysql table

function rs() {
  var arr = [];
  $('.cat').each(function() {

    arr.push($(this).text());
  });
  $.ajax({
    url: 'insert.php',
    data: {
      array: arr
    },
    type: 'POST',
    success: function() {
      alert("data has been sent");
      document.getElementById('exampleModal1').style.display = "none";
    }

  });
}

php file :

<?php

$con = mysqli_connect("localhost", "root", "", "test");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ".mysqli_connect_error();
}

// escape variables for security
//home tab
$cat1 = mysqli_real_escape_string($con, $_POST['array[0]']);

$cat2 = mysqli_real_escape_string($con, $_POST['array[1]']);

$cat3 = mysqli_real_escape_string($con, $_POST['array[2]']);

$cat4 = mysqli_real_escape_string($con, $_POST['array[3]']);

$cat5 = mysqli_real_escape_string($con, $_POST['array[4]']);

$sql1 = "INSERT INTO rs (Category)
VALUES('".$cat1."'), ('".$cat2."'), ('".$cat3."'), ('".$cat4."'), ('".$cat5."')
";

if (!mysqli_query($con, $sql1)) {
  die('Error: '.mysqli_error($con));
}

echo "1 record added";

mysqli_close($con);
?>

but the problem is that , the said is not passed. table shows blank fields. i do not know what i am doing wrong. i did not find any solution yet.

24
  • Are you getting any result for these $cat1 variables? Commented Feb 19, 2018 at 7:33
  • just store these variables in mysql and later retrive to show the entry Commented Feb 19, 2018 at 7:35
  • Im not much expert in php. I just want know is, whether you are getting those posted values to you php file or not? Commented Feb 19, 2018 at 7:37
  • 1
    format is correct as i want to store as individual entry Commented Feb 19, 2018 at 7:49
  • 1
    Try looping through the data in the PHP (foreach loop), this will also cut down the amount of code and allow for more than 5 entries, which then could be reused. So, for example foreach($_POST as $key => $value) and use $value as each category. It might be worth checking you have data there too. Commented Feb 19, 2018 at 9:19

0

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.