php message displays a blank message and no output

advertisements

I am trying to send a group of variables and using PDO to insert into my database, however when i run the page, i get an empty message box and nothing is happening on the page.

This is how I am getting the user inputs.

<div class="caption1" contentEditable ="plaintext-only" id="edit1a">
        <P> COMPANY ON AMAZON WANTS YOU TO TEST </BR> AND REVIEW IT’S PRODUCT!</P>

index.html

<button id="save">Click to Save</button> 

<script type="text/javascript">
        $(document).ready(function(argument) {
            $('#save').click(function(){
                // Get edit field value
                $edit1a = $('#edit1a').html();
                $edit2a = $('#edit2a').html();
                $edit3a = $('#edit3a').html();
                $edit4a = $('#edit4a').html();
                $edit5a = $('#edit5a').html();
                $edit6a = $('#edit6a').html();
                $edit7a = $('#edit7a').html();
                $edit8a = $('#edit8a').html();
                $edit9a = $('#edit9a').html();
                $edit10a = $('#edit10a').html();
                $edit11a = $('#edit11a').html();
                $.ajax({
                    url: 'get.php',
                    type: 'post',
                    data: {edit1: $edit1a, edit2: $edit2a, edit3: $edit3a, edit4: $edit4a, edit5: $edit5a, edit6: $edit6a, edit7: $edit7a, edit8: $edit8a, edit9: $edit9a, edit10: $edit10a, edit11: $edit11a,},
                    datatype: 'html',
                    success: function(rsp){
                            alert(rsp);
                        }
                });
            });
        });
    </script>

get.php

<?php
//configuration

$host = "localhost";
$name = "lpages";
$user = "root";
$pass = "****";

//connection
$odb = new PDO("mysql:host=".$host.";dbname=".$name,$user,$pass);

//new data
if(isset($_POST['edit1a'])){
$edit1 = $_POST['edit1a'];
$edit2 = $_POST['edit2a'];
$edit3 = $_POST['edit3a'];
$edit4 = $_POST['edit4a'];
$edit5 = $_POST['edit5a'];
$edit6 = $_POST['edit6a'];
$edit7 = $_POST['edit7a'];
$edit8 = $_POST['edit8a'];
$edit9 = $_POST['edit9a'];
$edit10 = $_POST['edit10a'];
$edit11 = $_POST['edit11a'];
$q      = "INSERT INTO webpages1(edit1, edit2, edit3, edit4, edit5, edit6, edit7, edit8,     edit9, edit10, edit11)
            values (:edit1, :edit2, :edit3, :edit4, :edit5, :edit6, :edit7, :edit8,     :edit9, :edit10, :edit11);";
            $query = $odb->prepare($q);
            $results = $query->execute(array(
                  ":edit1"=>$edit1,
                  ":edit2"=>$edit2,
              ":edit3"=>$edit3,
              ":edit4"=>$edit4,
              ":edit5"=>$edit5,
              ":edit6"=>$edit6,
              ":edit7"=>$edit7,
              ":edit8"=>$edit8,
              ":edit9"=>$edit9,
              ":edit10"=>$edit10,
              ":edit11"=>$edit11
              ));
        }
        ?>

I am fairly new to using PDO so maybe I am having an issue with the formatting


I think your POST keys dont match.

You are sending

{edit1: $edit1a, edit2: $edit2a,....

But in PHP you retrieve:

$edit1 = $_POST['edit1a'];

It should be edit1, edit2 and so on ...


But the most important is to know how to debug:

$odb = new PDO("mysql:host=".$host.";dbname=".$name,$user,$pass);
$odb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

otherwise you are clueless. You can also add more logic with the $result value.

for example:

if(!$results){
   var_dump($query->error());
   exit();
}

As for the undefined value, PHP can tell you if you turn on error reporting:

error_reporting(E_ALL);
ini_set('display_errors', '1');

And of course don't forget to send response back to the ajax call back to determine success.