0

I'm making a address book in php / sql / ajax currently I have this :

I'm loading the selected contact's informations with ajax like this :

 $.ajax({
                    url: "select.php",
                    dataType: "html",
                    type: "post",
                    data: {
                        "id": id
                    },
                    success: function(Result) {
                        $('#result').html(Result);
                    }
                });

but my problem is to edit the contact inforamtions. in the select.php which get the contact id to select from the db and display it, I added many textbox allowing us to edit the datas but when i press the button submit it doesn't do anything.

`

if(isset($_POST['update']))
{
    $prenom=$_POST["prenom"];
    $nom=$_POST["nom"];
    $tel=$_POST["tel"];
    $ville=$_POST["ville"];
    $email=$_POST["email"];
    //$q= "UPDATE `flexyperso`.`ca11` SET `prenom` = '".$name."', `nom` = '".$nom."', `tel` = '".$tel."', `ville` = '".$ville."', `email` = '".$email."' WHERE contact_id = '2'";
    echo "button pressed ";
    mysql_query($q);
}

`

Does anyone know why the button detection don't work ?

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Nalik
  • 21
  • 2
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard May 31 '17 at 14:43
  • You're only sending `id`, so you never enter the `if` statement `if(isset($_POST['update']))` You will need to send the other data too. – Jay Blanchard May 31 '17 at 14:44
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 31 '17 at 14:45
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 31 '17 at 14:45
  • because `$_POST['update']` does not exist in the request... even if it did the update would not happen you commented it out – Masivuye Cokile May 31 '17 at 14:45
  • If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard May 31 '17 at 14:55

1 Answers1

0

You're only sending id, so you never enter the if statement if(isset($_POST['update'])) You will need to send the other data too.

data: {
    "update" : update,
    "id" : id,
     // other data here - nom, tel, etc.
  },
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Oh I can't check directly in the select.php where the button is located? – Nalik May 31 '17 at 14:58
  • Not as long as you're using AJAX to send the data. If you're using AJAX you have to define each bit of data you want to send or you can serialize the form. https://api.jquery.com/serialize/ – Jay Blanchard May 31 '17 at 15:00
  • But all the form is located in the select.php which I loaded with ajax. Does it mean I have to put this form inside my main page? – Nalik May 31 '17 at 15:05
  • Ideally your form is in an HTML file, your JavaScript is in its own file and your PHP is in another file. You would include your JavaScript in your HTML file and then "call out" to the PHP with AJAX. – Jay Blanchard May 31 '17 at 15:07