Search code examples
phpjqueryajaxformssend

Send one data in ajax don't work


I am trying send one data to a data base, I am using the same structure that others, and this others works fine.

Form

<form action="" method="post" class="col-md-12">
  <select id="formu" name="cars" multiple " class="col-md-12">
    <option selected="selected">Multiple</option>
    <option>Multiple2</option>
    <option selected="selected">Multiple3</option>
  </select>
  <input id="send" class="col-md-12" type="submit" value="submit">
</form>

ajax

function showValues() {
  var str = $( "form" ).serialize();
  alert(str)
  debugger;
  $.ajax({  
    url: "recog_use.php",
    data: {
    'cars': str
    },
    type: 'GET'          
  });
} $( "form" ).on( "submit", showValues );

recog_use.php

include "conexion_leer.php";

$v='amigos';
$sql = "INSERT INTO `{$v}` (nombre)
VALUES ({$_GET['cars']}')";

if (mysqli_query($conn, $sql)) {
 header('Location:../../Usuarios/mapa.html'); 
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
 mysqli_close($conn);

the file conexion_leer.php have the connection to DB

thanks you


Solution

  • Try with this

    function showValues() {
         var form_data = $( "form" ).serialize();
         $.ajax({  
              url: "recog_use.php",
              data: form_data,
              type: 'GET'          
         });
    } 
    $( "form" ).on( "submit", showValues );
    

    in PHP Side

    include "conexion_leer.php";
    $v='amigos';
    $sql = "INSERT INTO `".$v."` (nombre) VALUES ('".$_GET['cars']."')";