Reputation: 1547
I want to pass an array into a database but i'm getting a notice and the echo is not working to print out the string I want.
Notice: Array to string conversion on line 7 ( echo $item; )
How do I capture the data being sent into a database?
Where the arrays are sent by author and their books. e.g.
[{"name":"Book1","books":["Book1.1"]},{"name":"Book2","books":["Book2.1","Book2.2"]}]
This is next.php where the data is being passed and not being echoed.
<?php
var_dump($_POST);
$data = json_decode(stripslashes($_POST['arr']), true);
foreach($data as $item){
echo $item;
// insert to db
}
?>
The alert is giving me the return data from next.php in the sendToServer function (see snippet).
success: function(data) {
alert(data);
}
Full code in the snippet:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">
<!-- #main {
max-width: 800px;
margin: 0 auto;
}
-->
</style>
</head>
<body>
<div id="main">
<h1>Add or Remove text boxes with jQuery</h1>
<div class="my-form">
<!-- <form action="next.php" method="post">-->
<button onclick="addAuthor()">Add Author</button>
<br>
<br>
<div id="addAuth"></div>
<br>
<br>
<button onclick="submit()">Submit</button>
<!-- </form>-->
</div>
<div id="result"></div>
</div>
<script type="text/javascript">
var authors = 0;
function addAuthor() {
authors++;
var str = '<br/>' + '<div id="auth' + authors + '">' + '<input type="text" name="author" id="author' + authors + '" placeholder="Author Name:"/>' + '<br/>' + '<button onclick="addMore(\'auth' + authors + '\')" >Add Book</button>' + '</div>';
$("#addAuth").append(str);
}
var count = 0;
function addMore(id) {
count++;
var str =
'<div id="bookDiv' + count + '">'
+ '<input class="' + id + '" type="text" name="book' + id + '" placeholder="Book Name"/>'
+ '<span onclick="removeDiv(\'bookDiv' + count + '\')">Remove</span>'
+ '</div>';
$("#" + id).append(str);
}
function removeDiv(id) {
$("#" + id).slideUp(function() {
$("#" + id).remove();
});
}
function submit() {
var arr = [];
for (i = 1; i <= authors; i++) {
var obj = {};
obj.name = $("#author" + i).val();
obj.books = [];
$(".auth" + i).each(function() {
var data = $(this).val();
obj.books.push(data);
});
arr.push(obj);
}
sendToServer(arr);
//$("#result").html(JSON.stringify(arr));
}
function sendToServer(data) {
$.ajax({
type: "POST",
data: {
arr: JSON.stringify(data)
},
url: "next.php",
success: function(data) {
alert(data);
}
});
}
</script>
</body>
</html>
Upvotes: 2
Views: 3039
Reputation: 1528
This is what you have form POST:
[
{
"name":"Book1",
"books": [
"Book1.1"
]
},{
"name":"Book2",
"books": [
"Book2.1",
"Book2.2"
]
}
]
To read the data using a foreach you need to do:
foreach($data as $item) {
echo "Name: " . $item["name"] . "<br>";
echo "Books: " . implode(", ", $item["books"]) . "<br>";
// Other way..
echo "Name: " . $item["name"] . "<br>";
echo "Books: <br>";
foreach($book as $item["books"])
echo $book . "<br>";
}
By this foreach you can manipulate your data and build your INSERT statement to input data to database.
Upvotes: 2
Reputation: 823
you are getting Notice: Array to string conversion on line 7 ( echo $item; ) because you are going to print array. You can't print array whereas you can do a string so. To do like that use something like this.
<?php
var_dump($_POST);
$data = json_decode(stripslashes($_POST['arr']), true);
for ($i=0;$i<count($data);$i++){
echo "Name: ".$data[$i]['name']. "<br/>";
echo "Books: ";
foreach($data[$i]['books'] as $book){
echo $book . " ";
}
echo "...............<br/>";
// insert to db example
$sql="INSERT INTO author(id, name) VALUES (NULL, '".$data[$i]['name']."')";
//execute query and get last insert id
//$author_id=last insert id
foreach($data[$i]['books'] as $book){
$sql="INSERT INTO author_books(id, author_id, book_name) VALUES (NULL,'$author_id', '$book')";
//execute query
}
}
?>
Upvotes: 2