Insert array from ajax into mysql database

Asked

Viewed 65 times

2

I have this ajax that shows the items chosen in "checkbox" form.

$(document).ready(function(){  
      $('#submit').click(function(){  
           var languages = [];  
           $('.get_value').each(function(){  
                if($(this).is(":checked"))  
                {  
                     languages.push($(this).val());  
                }  
           });  
           languages = languages.toString();  
           $.ajax({  
                url:"form_cadastro.php",  
                method:"POST",  
                data:{languages:languages},  
                success:function(data){  
                     $('#result').html(data);  
                }  
           });  
      });  
 });

Html

<input type="checkbox" value="1"   class="get_value" name="redes_sociais">
<input type="checkbox" value="1"  class="get_value" name="site">
<input type="checkbox"  value="1" class="get_value" name="endereco">

<button type="submit" id="submit" class="btn btn-primary">Cadastrar</button>

PHP

if(isset($_POST["languages"]))  
 {  

      echo $_POST["languages"];  
 }  

My question is how can I enter the values of each input into the mysql database?

Mysql structure

$sql = "INSERT INTO planos (redes_sociais, site, endereco) VALUES

('".$_POST['redes_sociais']."', '".$_POST['site']."',".$_POST['endereco']."')";
  • When you send the parameter Languages via ajax, it will match the post, knowing this, I need to know what the value of $_POST["Languages"] to help you.

  • this in my question I edited her

  • I’m not sure what you want, in the "Mysql Structure" part, what exactly do you want to do? > My question is how to insert into BD .

  • is that the values of the checkbox appears only with that echo $_POST["Languages"]; how could I insert into my mysql?

  • That’s exactly what I want to know, what the value of $_POST["Languages"], is not in your post, and, you’re treating this in JS, sending as a single parameter to PHP.

  • the value will always be "1" when selected. <input type="checkbox" value="1" class="get_value" name="social redes_social">

Show 1 more comment

1 answer

3


Basically the way you treat JS is wrong.

Your code

$(document).ready(function(){  
      $('#submit').click(function(){  
           var languages = [];  
           $('.get_value').each(function(){  
                if($(this).is(":checked"))  
                {  
                     languages.push($(this).val());  
                }  
           });  
           languages = languages.toString();  
           $.ajax({  
                url:"form_cadastro.php",  
                method:"POST",  
                data:{languages:languages},  
                success:function(data){  
                     $('#result').html(data);  
                }  
           });  
      });  
 });

New code

$(document).ready(function(){  
      $('#submit').click(function(){  
           $.ajax({  
                url:"form_cadastro.php",  
                method:"POST",  
                data:$("input").serialize(),  
                success:function(data){  
                     $('#result').html(data);  
                }  
           });  
      });  
 });

Then you can treat in PHP as $_POST["atributo"]

redes_sociais=1&site=1&endereco=1

You can change $("input").serialize() for $(this).serialize()

  • is returning this in the console Uncaught Syntaxerror: Unexpected string

  • This error must be due to some wrong typing, see the example in jsfiddle. https://jsfiddle.net/0tk2mq4q/ Show your new code as changed.

  • @Mauroalexandre, exactly what he needed, just one little mistake, which are the keys on data, remaining ta running smooth

  • Putz! I forgot to remove. Edited.

  • I even deleted my answer, that solves everything. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.