Reputation:
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];
});
</script>
<?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?
Upvotes: 1
Views: 230
Reputation: 7032
Array.prototype.sum = function(){
for(var i=0,sum=0;i<this.length;sum+=this[i++]);
return sum;
}
$(document).ready(function() {
var arr = $(".as-values").val().split(",");
$.post('something.php', {'sum':arr.sum()}, function() {
//callback success code here
});
});
Upvotes: 0
Reputation: 11175
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
AJAX
$.post("phpscript.php", {
cat1: category_1,
cat2: category_2,
cat3: category_3,
});
PHP
$query = $mysqli->prepare("INSERT INTO table VALUES (?, ?, ?)");
$query->bind_param('sss', $val1, $val2, $val3); // get these from $_POST
$query->execute();
Upvotes: 2
Reputation: 69905
Use ajax to post these 3 values and write a code on the server side page to insert into DB.
Jquery AJAX
Upvotes: 0