Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having issues posting JS object into a PHP page on submit and then store it into mySQL db.

Here's the script:

var idToDb = [];
var nameToDb = [];
var friendToDb ={};
$('.btn-add').click(function(e){
    e.preventDefault();
    $(this).toggleClass('btn-info btn-success');
    $(this).toggleClass('friend-selected');
    $(this).html($(this).html() == "Selected" ? "Add" : "Selected");         
    addOrRemove(idToDb, $(this).parent().data('id'));
    addOrRemove(nameToDb, $(this).parent().data('name'));
    friendToDb = _.object(idToDb, nameToDb)

I want to post this array "friendToDb" to a separate PHP page using post after clicking submit button and then store it in mySQL db.

Here's what I'm trying to post in PHP:

<form method="post" action="friend_input.php">
    <div id="submitBtnRow" class="row top-plus">
      <div class="col-md-12">
        <div class="form-group">
          <input type="submit" id="submitBtn" class="btn btn-info btn-lg btn-block" value="SUBMIT">Submit</a>
        </div>
      </div>
    </div><!-- /submitBtnRow -->
</form>

And then in friend_input.php, I'm trying to fetch friendToDb and store in mySQL db and this I'm not sure how to do.

Please advice me how this should be done.

Thank you.

share|improve this question
1  
Have you heard of $.ajax?.. –  Joke_Sense10 Oct 14 '13 at 16:38
 
What issues are you having? Where is the code that tries to submit it? –  Barmar Oct 14 '13 at 16:38
 
If i am not wrong friendToDb is an object not an array.Your code says so. –  Joke_Sense10 Oct 14 '13 at 16:44
 
@NabeelSheikh Yeah sorry that's an object. And I'm not aware of ajax at all. –  rrg Oct 14 '13 at 16:46
 
@Barmar Sorry I forgot to include that. I've updated my question. –  rrg Oct 14 '13 at 16:52
add comment

2 Answers

Add this to your form

<input id="friendDb" name="friendDb" type="hidden" />

Then using use

$("#friendDb").val();

To set the value before posting to your script.

share|improve this answer
 
Thanks. But what is the connection between <input> tag and setting the variable $("#friendDb").val();? I followed your approach and changed the input tag and used another variable to store #friendToDb and tried to echo it in php. But nothing was displayed. –  rrg Oct 14 '13 at 20:10
add comment

Make form action="" and Use ajax to pass your object to php:

$.ajax({
url:"friend_input.php",
type:"post",
data:{value:JSON.stringify(friendToDb)},   //Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
success:function(result){
// response from php page
}
});

In php page:

echo json_decode($_POST['value']);  //Takes a JSON encoded string and converts it into a PHP variable.
share|improve this answer
 
Thanks for this, but how do I make sure that this happens only after I click the submit button? And could you also explain what does "//response from php page" means? Thanks –  rrg Oct 14 '13 at 17:08
 
Use this function once friendToDb is declared. i.e after assigning value to friendToDb. "//response from php page" means the response that your getting from your php page.i.e in this case $_POST['value'] will be displayed if you alert variable result –  Joke_Sense10 Oct 14 '13 at 17:17
 
Ok. So I did what you told me and after I click the submit button, the page gets redirected to friend_input.php and I get an error: "Undefined index: value in C:\xampp\htdocs\project-1\friend_input.php on line 37" –  rrg Oct 14 '13 at 17:38
 
Is it getting redirected via ajax or form action?? –  Joke_Sense10 Oct 14 '13 at 17:44
 
It should get redirected via form action because the variable friendToDb is dynamic and is not final until user clicks submit button. –  rrg Oct 14 '13 at 17:46
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.