Using .Split() then adding these values to 3 php variables


I have used this code on an autosuggest script to split the CSV created into 3 values in an array as such:

<?php if($_POST['category_submit']){ ?>
<script type="text/javascript">
$(document).ready(function() {
var arr = $(".as-values").val().split(",");
var category_1=arr[0];
var category_2=arr[1];
var category_3=arr[2];
<?php } ?>

I now want to add the 3 values within the 3 ‘var’ into the MySQL database. What would be the steps necessary to do so?


You would have to use $.ajax to call a PHP insert script. Remember, if you’re going to use PHP to insert, make sure you’re using Prepared Statements


$.post("phpscript.php", { 
    cat1: category_1,
    cat2: category_2,
    cat3: category_3,



$query = $mysqli->prepare("INSERT INTO table VALUES (?, ?, ?)");
$query->bind_param('sss', $val1, $val2, $val3); // get these from $_POST

Answered By – Phil

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published