2

i try to update/edit using jquery and then save it to database with the help of php

the problem is i cant pass the new data to php or php didnt receive data from jquery

here is my form

<form method="POST" name="postForm" action="update.php">
<div id="edit">
First name:<input type="text" id="firstname" value="<?php echo $row['firstname']; ?>">
</div>
</form>
<button id="button">Edit</button>

and this is my script of jquery

jQuery("#edit").dialog({
    modal: true,
    resizable: false,
    draggable: false,
    autoOpen: false,
    width: 400,
    buttons: [
        {
            text: "Ok",
            click: function() {
                jQuery.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "update.php",
                    async:true,
                    data: { dataToUpdate: jQuery('#edit').attr('value') }
                });
            }
        },
        {
            text: "Cancel",
            click: function() {
                jQuery(this).dialog( "close" );
            }
        }
    ]
});

// Link to open the dialog
jQuery("#button").click(function(event) {
    jQuery("#edit").dialog("open");
    event.preventDefault();
});

and this is my PHP

  $id = $_GET['id']; // i dont know if i should put it or not to identify the info i want to edit
  $firstname = $_POST['firstname'];

  $sql = "UPDATE info SET
        firstname='$firstname' WHERE id=$id";

        $result = mysql_query($sql);

        echo json_encode($result);
1
  • First of all do not use mysql_* functions anymore. They are marked as deprecated and in PHP7 they are completely removed. After that your sql query is vulnerable and not secure at all. Please make sure that the values to insert are escaped. Commented Jul 14, 2016 at 10:15

4 Answers 4

1

Your problem is here: data: { dataToUpdate: jQuery('#edit').attr('value') }

Correct: data: { firstname: $('#firstname').val() }

Now it should works

Sign up to request clarification or add additional context in comments.

Comments

0

You have missed the name attribute in your form's input tag that's why PHP didn't receive the variable...

<input type="text" id="firstname" name="firstname" value="<?php echo $row['firstname']; ?>">

Or make sure you are passing the value properly using JQuery.

3 Comments

so this is what should i put on data: { "firsname":jQuery("#firstname").val()} ? how about my PHP is it correct does it receive the post?
It should. remove the dataType: "json" from jquery.
Yes its proper. You need an ID as well in order to determine which row you need to update and also follow @Marcel's comment and its important...
0
jQuery('#edit').attr('value') } 

it's div that doesn't contain value attribute, use this selector:

jQuery('#edit').attr('firstname') 

2 Comments

Whoops!!, it should be jQuery('#firstname').attr('value')
how about this one data: { "firsname":jQuery("#firstname").val()} ? is it correct too?
0

i got it the correct answer for this is i change the

data: { dataToUpdate: jQuery('#edit').attr('value') }

of jQuery script into

data: { "firsname":jQuery("#firstname").val()}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.