0

So I have this form:

echo '<form id="target" action="edit-property.php?Property='.$property.'" method="post">';
echo "Address: ".'<input type="text" name="address" value="'.$propAddress.'" />';
echo '<input type="submit" value="Update">';
echo "</form>";

And on submission I change the database entry with this function (I'm still learning the change from mysql to mysqli, so im not sure if i've done it right and I don't totally know how to bind params when it comes to updating a table, but thats not my question)

$property = $_GET['Property']
if (isset($_POST['address']) && !empty($_POST['address'])) {
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    if (!$mysqli) {
        die('There was a problem connecting to the database.');
    }
    else {
        $address = mysqli_real_escape_string($mysqli, $_POST['address']);
        $id = intval($property);
        if($id) {
        $query =("UPDATE Properties
            SET Address = '$address'
            WHERE Id = '$id'");
            if (mysql_query($query)){
                echo '1';
                exit;
            }
            else {
                echo '0';
                exit;
            }
            $mysqli->close(); 
        }
    }
}

Alrighty. So that works ok (please feel free to correct me if i've done something stupid here, I find that I learn best through the criticism and stackoverflow has helped me heaps in improving!)

But here's my dilemma. I want to update on page without having to refresh the page.

I came across the code below, modified it to suit me and unfortunately it isn't working. I'm afraid I don't know why:

<script>
var id = <?=$id?>;
// when the DOM is ready
$(document).ready(function() {
    $('#target').submit(function(e) {
        // prevent normal, boring, tedious form submission
        e.preventDefault();
        // send it to the server out-of-band with XHR
        $.post('edit-property.php?Property=' + id, function() {
            data: $(this).val(),
            success: function(resp) { 
                if(resp == '1') {
                    alert('Saved successfully');
                } else {
                    alert('Oops, something went wrong!');
                }
            }
        });
    });
});
</script>

It's a mystery unfortunately - Can't get it working. What could I be doing wrong?

Thank you :)

David G
  • 6,803
  • 4
  • 28
  • 51
  • What "isn't working" about the bottom code? – James Webster Feb 03 '13 at 09:23
  • Sorry, to clarify. Nothing happens. The page doesn't 'refresh' so to speak. And the alerts don't appear. I have to manually refresh the page to see changes. But the code seems fine :/ – David G Feb 03 '13 at 09:24
  • 1
    @Akam: As of PHP 5.4, short tags are supported everywhere. There was a lot of talk on the dev list about short tags and there is even this stack post too: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Tigger Feb 03 '13 at 09:26
  • Sorry, is said "short tags", but I meant the echo short tag, `= ... ?>`. – Tigger Feb 03 '13 at 09:33

1 Answers1

2

I think you've misunderstood the syntax of this call and you have wrapped some of the parameters intended for the $.post into a superfluous "function" that your probably expected to use for completion.

$.post('edit-property.php?Property=' + id, 
            /*function() {*/
            data: $(this).val(),
            success: function(resp) { 
                if(resp == '1') {
                    alert('Saved successfully');
                } else {
                    alert('Oops, something went wrong!');
                }
            /*}*/
        });
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • The second argument is an object and the third is anonymous: ...{data: $(this).val()}, function() {... – Nick Feb 03 '13 at 09:32
  • Ah damn. Could you recommend a better way of achieving what I'm after? I'm somewhat out of my element. – David G Feb 03 '13 at 09:55
  • Copying the code I have there should at least give you a response as I commented the faulty code. – James Webster Feb 03 '13 at 10:46