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>