Julian Koster
Julian Koster

Reputation: 556

Ajax & PHP Submitting

I am quite new to AJAX and it has never really worked out, however I would like to finally get my head around it. That's why I'm asking you guys for help! After reading hundreds of answers here over the years I have finally gotten to the point where I cannot figure it out.

I have a simple Jquery UI popup that displays a form to be filled:

<form>
<div id="Dialog2">
          <p>Nieuwe Klant</p>
          <table width="100%" border="0">
            <tr>
              <th scope="row">Naam:</th>
              <td><input name="nc_name" type="text" id="nc_name" tabindex="1" /></td>
            </tr>
            <tr>
              <th scope="row">Type:</th>
              <td><select name="nc_type" id="nc_type" tabindex="2">
                <option>Bedrijf</option>
                <option>Particulier</option>
              </select></td>
            </tr>
            <tr>
              <th scope="row">Adres:</th>
              <td><input name="nc_adres" type="text" id="nc_adres" tabindex="3" /></td>
            </tr>
            <tr>
              <th scope="row">Postcode:</th>
              <td><input name="nc_zip" type="text" id="nc_zip" tabindex="4" /></td>
            </tr>
            <tr>
              <th scope="row">Land:</th>
              <td><input name="nc_country" type="text" id="nc_country" tabindex="5" /></td>
            </tr>
            <tr>
              <th scope="row">Stad:</th>
              <td><input name="nc_city" type="text" id="nc_city" tabindex="6" /></td>
            </tr>
            <tr>
              <th scope="row">KVK Nummer:</th>
              <td><input name="nc_kvk" type="text" id="nc_kvk" tabindex="8" /> <input name="kvkzoek" type="button" id="kvkzoek" tabindex="7" value="Zoek Automatisch" onclick="seeKVKnumber()" /></td>
            </tr>
            <tr>
              <th scope="row"><div id="info" /></th>
              <td><input name="submit_two" type="button" id="submit_two" tabindex="9" value="Klant Toevoegen" /></td>
            </tr>
          </table>
          <p><br />
          </p>
        </div>
        </form>

Which is handled by the following piece of script in the header of the same page:

$(document).ready(function(){
                    $("#submit_two").click(function(){

                          var name=$("#nc_name").val();
                          var type=$("#nc_type").val();
                          var adres=$("#nc_adres").val();
                          var zip=$("#nc_zip").val();
                          var country=$("#nc_country").val();
                          var city=$("#nc_city").val();
                          var kvk=$("#nc_kvk").val();

                          $.ajax({
                              type:"get",
                              url:"addcustomer.php",
                              data:"name="+name+"&type="+type+"&adres="+adres+"&zip="+zip+"&country="+country+"&city="+city+"&kvk="+kvk,
                              success:function(data){
                                 $("#info").html(data);
                              }

                          });

                    });
               });

I have used "POST" as well as "GET" but both do not work. The php page that handles the INSERT into my database looks like this:

<?
include('../includes/dbconfig.php');

    $name=$_GET["name"];
    $type=$_GET["type"];
    $adres=$_GET["adres"];
    $zip=$_GET["zip"];
    $city=$_GET["city"];
    $kvk=$_GET["kvk"];
    $country=$_GET["country"];

    //Insert query
    $query = mysqli_query($con,"insert into customers(name, address, zipcode, city, kvk, country) values ('$name','$type','$adres','$zip','$city','$kvk','$country')");
if($query){
    echo "Your comment has been sent";
  }
  else{
    echo "Error in sending your comment";
    var_dump($_GET);
  }
mysqli_close($con); // Connection Closed
?>

Every time I fill in the form I get the error: "Error in sending your comment".

Even the var_dump($_GET) displays the proper values:

array(7) { ["name"]=> string(5) "MrBla" ["type"]=> string(7) "Bedrijf" ["adres"]=> string(6) "Blabla" ["zip"]=> string(7) "aaaabla" ["country"]=> string(6) "blabla" ["city"]=> string(3) "bla" ["kvk"]=> string(3) "bla" }

But the values just do not appear in the database. Mozilla's Firebug also doesn't display any errors or breaks.

I was hoping any of you code wizzards could help me out here?

Much appreciation in advance and thank you all for the many useful answers you have provided to others over the years.

Upvotes: 0

Views: 68

Answers (2)

Giwwel
Giwwel

Reputation: 347

You have an error in your SQL-Syntax. I think it's the different count of columns in the INSERT INTO-Statement. Please remove the $type from the VALUES-part, or add the type in the COLUMN definition-part.

For next time if you have a similar problem, please post the mysqli_error output. :)

Upvotes: 1

vaso123
vaso123

Reputation: 12391

You forget to add column type in your query:

$query = mysqli_query($con,"insert into customers(name, address, zipcode, city, kvk, country) values ('$name','$type','$adres','$zip','$city','$kvk','$country')");

should be

$query = mysqli_query($con,"insert into customers(name, type, address, zipcode, city, kvk, country) values ('$name','$type','$adres','$zip','$city','$kvk','$country')");
  • Avoid sql injection by escaping your variables.

Upvotes: 1

Related Questions