1

I want to add value 'Nova parcela' to database in table zemljiste so I write first ajax code:

<script>
var nova_parcela = 'Nova parcela';
 $("#dodaj").click(function() {
        $.ajax({
            url: "insert.php",
            type: "POST",
            async: true, 
            data: { name:nova_parcela}, //your form data to post goes here as a json object
            dataType: "html",

            success: function(data) {
                $('#output').html(data);
                drawVisualization();   
            },  
        });

});
</script>

after that I write php code: INSERT.php is:

    if ($_SERVER['REQUEST_METHOD'] == "POST") {

 if (!$_POST['name']!='Nova parcela') {
            echo "<p>Popunite sva polja</p>";
            exit;
        } else {
            try {        
                $DBH = new PDO($dsn, $user, $pass, $opt);
                $STH = $DBH->prepare("INSERT INTO zemljiste (naziv) VALUES (:name)");

                $STH->bindParam(':name', $_POST['name']);

                $STH->execute();

            } catch (PDOException $e) {
                echo $e->getMessage();
            }
            echo "<p>Data submitted successfully</p>".$_POST['ajdi'];

        }

    }

    $DBH = null;

but nothing happend , what can be a problem here?

2
  • What are you intending to do here if (!$_POST['name']!='Nova parcela')? Commented Mar 12, 2014 at 15:02
  • ok, that is no so imortant to me so i can delete ths part of code Commented Mar 12, 2014 at 15:04

1 Answer 1

1

try change that

   if (!$_POST['name']!='Nova parcela') {

to

  if (!isset($_POST['name'])) {

EDIT:

if (isset($_POST['name'])) {

        try {        
            $DBH = new PDO($dsn, $user, $pass, $opt);
            $STH = $DBH->prepare("INSERT INTO zemljiste (naziv) VALUES (:name)");

            $STH->bindParam(':name', $_POST['name']);

            $STH->execute();
            $datas['msg']= "success" ;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }

      $DBH = null;
     echo json_encode($datas);
    }

and your script:

  <script>
 var nova_parcela = 'Nova parcela';
  $("#dodaj").click(function() {
    $.ajax({
        url: "insert.php",
        type: "POST",
        async: true, 
        data: { name:nova_parcela}, //your form data to post goes here as a json object
        dataType: "json",

        success: function(data) {
               if (data.msg == 'success'){
                   $('#output').html("<p>Data submitted successfully</p>"+nova_parcela);
                   drawVisualization();   
                  }
            else{
         $('#output').html("<p>Popunite sva polja</p>");
              }
        }  
    });

 });
 </script>
Sign up to request clarification or add additional context in comments.

7 Comments

is anything inserted in the database? when not try an print_r($_POST); in the insert.php when yes make a alert(data); in the success function and look the page returns
also I run php code without error hadling but again I just get white screen
are you making the submit to insert.php ? show your html form.
no I dont have a form, I have only var nova_parcela, that value I need to add to database
in html i have only this: <button class="btn btn-success" id="dodaj"><i class="fa fa-plus"></i> Dodaj novu parcelu</button></div>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.