And how to properly separate the events in the handler? Here is the code:

<?session_start(); error_reporting(0);?> <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Простой пример использования AJAX</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> function SendRequest(){ $.ajax({ type: "POST", url: "obrabotchik.php", data: "sid=<?=session_id()?>&data_1="+$('#data_1').val()+"&data_2="+$('#data_2').val()+"&db="+$('#db').val(), success: function(response1){ $('#response1').html(response); } }); }; function UpdateRequest(){ $.ajax({ type:"POST", url: "obrabotchik.php", data: "sid=<?=session_id()?>&before="+$('#before').val()+"&after="+$('#after').val(), success: function(response){ $('#response2').html(response2); } }); }; </script> </head> <body> <table> <tr> <th>Запрос</th> <th>Ответ сервера</th> </tr> <tr> <td> <label>Переменная 1: <input type="text" size="10" id="data_1" /></label><br /> <label>Переменная 2: <input type="text" size="10" id="data_2" /></label><br /> <label><input type="text" id="db"></label> <button onclick="SendRequest();">Послать запрос</button> </td> <td><div id="response1"></div></td> </tr> <tr> <td> <label>Что заменить? :<input type="text" size="10" id="before" /></label><br /> <label>На что Заменить: <input type="text" size="10" id="after" /></label><br /> <button onclick="UpdateRequest();">Обновить</button> </td> <td><div id="response2"> </div></td> </table> </body> </html> 

handler code:

 <? session_start(); $host = 'localhost'; $user = 'root'; $pswd = ''; $db = 'my_bd'; $connection = mysql_connect($host,$user,$pswd); mysql_set_charset('utf8',$connection); if(!$connection || !mysql_select_db($db,$connection)) { exit(mysql_error()); } if(session_id() != $_POST['sid']) die('Wrong Request'); if(isset($_POST['db']) && (!empty($_POST['db'])) ){ echo $_POST['db']; $s=$_POST['db']; $query = mysql_query("INSERT INTO mytabl(source) VALUES ('$s')"); echo "Статья успешно добавлена!"; } $before=$_POST['before']; $after=$_POST['after']; if(!empty($before) && !empty($after)){ $query = mysql_query("UPDATE mytabl SET source='$after' WHERE source=$before "); } ?> Значение переменной 1:<br /> <strong><?=$_POST['data_1']?></strong> <hr /> Значение переменной 2:<br /> <strong><?=$_POST['data_2']?></strong> <strong><?=$_POST['db']?></strong> 
  • @ Anatoliy sid = <? = Session_id ()?> This is a disaster and very open access to malware on behalf of an honest user - etki

1 answer 1

How hard it is = = (

In this situation, your project will not live long, if someone finds out in which cool file you need to send requests =)

It's easier to rewrite everything and make it more competently:

Create a form on the page (not essential, but it will be better)

Write a post request and send to the handler all the data from the form (serialize ())

This PHP handler can be demolished completely and write a new, higher quality one with checking all received data!

I can write a current example without a handler (in PHP, sort it out yourself, but it’s better to remove the current one)

For example, the page has a simple HTML form.

  <form name="form"> <input name="field" type="text" value="первый инпут"> <input name="field2" type="text" value="второй инпут"> <input name="field3" type="checkbox" value="чекбокс"> Выбрать <button type="submit">Отправить</button> <div id="error"></div> </form> 

We write a script

 <script> $(function(){ $('[name="form"]').submit(function(){ $('#error').html(''); var form = $('[name="form"]').serialize(); $.ajax({ url: "/obrabotchik.php", method: "POST", data: form, complete: function() {}, statusCode: { 200: function() { // в случае успеха (статус заголовка 200) выводим сообщение }, 403: function(jqXHR) { // 403, какая то ошибка, выводим сообщение в div#error var error = JSON.parse(jqXHR.responseText); $('#error').html(error.message); } } }); return; }); }); </script> 

And already PHP handler /obrabotchik.php

We accept the data in the object and not childishly process all the data, field by field (well, here you have to write a lot)

get an object in the form

 { field: 'первый инпут', field2: 'второй инпут', field3: 'чекбокс' }