Who can tell the best way to transfer data to the database without reloading the page.

For example, my worst option:

<script type="text/javascript" language="javascript"> function senddata() { $.ajax({ type: 'POST', url: "senddata.php", data: { name: $('#pagename').val(), url:$('#pagelink').val()}, success: function(data){notification('Страница создана')} } )} </script> 

In a separate file senddata.php there is a deputy request for adding information to the database. In this embodiment, the message "Page created" crashes regardless of whether infa was added to the database or not, but it crashes depending on whether a request was sent to the senddata.php file or not. Therefore, regardless of whether the record was in the database or not, it still gives the message "The page is created."

  • For this there is google. - Oleg Arkhipov
  • 2
    @kostya, judging by the avatar you are a hacker. Hackers should already know everything, by default. Especially not to ask such simple questions. - neoascetic
  • one
    Thank you all for the answers. Nevertheless, success will be better: function (data) {$ ('# alert'). Html (data)} Thus the answer to the sql query will be loaded into a container with # IDrt id. Well, with sql query already enough variations. - webkostya

4 answers 4

After adding to senddata.php, make a COUNT query to the database for the parameters you should have added. And if the output unit, it means that the data is added, if zero, then no.

 mysql_result(mysql_query("SELECT COUNT(*) FROM `baza` WHERE `name` = $_POST['name'] AND `url` = $_POST['url']"), 0); 

something like that

      $.post("senddata.php", {name: $('#pagename').val(), url:$('#pagelink').val() }, function(data) { alert("Data Loaded: " + data); }); 

      Yes, that's what happens. But it’s still worth checking whether the record is created and returning the result from senddata.php. The data argument, in your case, just contains everything that your script writes to the database.

      • How to check whether a record is created in the database? - webkostya
      • open the database file and view it, or view it in phpMyAdmin, or make a request to output this record from the database - dajver
      • on php, the mysql_last_insert_id ($ result) function returns the id of the inserted record, or null if the insertion failed. More clearly: $ result = mysql_query ("insert into someTable someValues"); $ id = mysql_last_insert_id ($ result); if ($ id) return true; return false; - MuFF

      When I did a similar thing, as the answer I transferred a peculiar status code. In your case I would do the same, i.e. The senddata.php page displays a message about successful or unsuccessful data writing (in my case it was 1 or 0). And then you process this message with js.

      • Tell me how. Because the senddata page always displays a message about a successful record, regardless of whether the record was added to the database or not. - webkostya