0

I habe built a cascading drop down that functions very well. when the form is submitted, the Id is passed to second script. I can´t seem to get the callback function to ...function properly. Don´t know what wrong.

note I updated the code with the suggested changes, but something still goes wrong.

The php:

<?php
if (!empty($_GET['id'])) {
$id = $_GET['id'];

try {


$objDb = new PDO('mysql:host=localhost;dbname=blankett', 'root', 'root');
$objDb->exec('SET CHARACTER SET utf8');

$sql = "SELECT * 
    FROM `forms`
    WHERE `id` = '$id'";
$statement = $objDb->prepare($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);



if (!empty($list)) {

   $out = array();

  foreach ($list as $row ) {
  $out[] = '<tr><td><a href="'.$row['link_form'].'">'.$row['name_form'].'</a></td> <td>'.$row['date_added'].'</td></tr>';
   }

  echo json_encode(array('error' => false, 'list' => $out));
} else {
  echo json_encode(array('error' => true));
}


} catch(PDOException $e) {
echo json_encode(array('error' => true));
}

}else {
echo json_encode(array('error' => true));
}


?>

The Jquery ajax call.

$('#blankett_form').submit(function() {
var id = $(this).find('.update:last').val();
    if (id == '') {
        alert('Välj land och region.'); //glöm inte bort att ändra beroende på land.
    } else {
        var table = '<table class="table table-hover table-bordered"><thead><tr><th>blanketter.</th><th>datum tillagt.</th></tr></thead><tbody></tbody></table>'
        $('#formsubmit').empty().append(table)
        $ajax({
                url: 'func/blankett_func2.php',
                data: {'id':id},
                dataType: 'JSON',
                success: function(data)
                {
                    $.each(data.list, function(index, value){
                    $('#formsubmit tbody').append(value);
                    });
                }
          });
return false
});
1
  • I think there is still a problem with the script since it does not load. What could be the reason? Commented Mar 24, 2013 at 0:36

2 Answers 2

1

Replace :

$each(data.out, function(){
    var trow = out;
    $('#formsubmit tbody').append(trow);
});

With :

$each(data.form, function(i, val){
    $('#formsubmit tbody').append(val);
});

as there is no out variable inside the each function, and it looks like you're naming the key form in the returned array ?

In the PHP you are redeclaring the array inside the loop, needs to be:

$out = array();

foreach ($list as $row ) {
     $out[] = '<tr><td><a href="'.$row['link_form'].'">'.$row['name_form'].'</a></td> <td>'.$row['date_added'].'</td></tr>';
}

and there's no need to pass an empty array to execute():

$statement->execute();
Sign up to request clarification or add additional context in comments.

2 Comments

You are right. I don´t know if it is because I am tired, but I can´t seem to get it to work. I guess I should not use form, because it is reserved work in Jquery?
Any idea how I should rewrite the php?
1

You arent sending the data properly, you have to send the id key and the id value e.g.

data: {'id':id},

Also your json output is of the form {"error": false, "form": "html string"} so to access the html string you have to use data.form instead of data.out. So try

$.each(data.form, function(index, value){
    $('#formsubmit tbody').append(value);
});

1 Comment

Totally right. Thank you! I still think there is something wrong in the script since it does not load properly.

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.