SOLVED: The code posted here was correct, but some of the PHP code connecting to the database needed fixing, I have it working now. I used Quentin's suggestions in the comments to fix my problem.
I'm trying to send a JSON of an array through ajax to a PHP file, then have the php file run. This is my ajax:
$('#submit').click(function() {
$.ajax({
method: "POST",
url: "submit.php",
data: {selectedImageArray: selectedImageArray}
}).done(function( msg ) {
alert( "Data Saved: " + selectedImageArray );
});
});
In my PHP file I connect to a database then have this:
$array = $_POST['selectedImageArray'];
$sql = $dbh->prepare("INSERT INTO pending_trades (steam_id, trade_items, is_giving, is_receiving, has_started) VALUES (:steamid, :itemlist, '1', '0', '0')");
$sql->bindParam(':steamid', $steamprofile['steamid']);
$sql->bindParam(':itemlist', $array);
$sql->execute();
I want this to send the "selectedImageArray" json to the php and run the msql query using it on the press of this button:
<button id="submit" class="button1" >Submit<span></span></button>
When the button is clicked, I get the "data saved: array data
" message, but the php code isn't run. Why isn't this working?
EDIT: I set data: {selectedImageArray: selectedImageArray}
instead of data: selectedImageArray
because I was told it makes it into JSON format, is that correct?