Good day. I want to send the values ​​from the form to the server, write them to the database and get a simple answer.

index.php <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#but1").click(function(){ $("#par1").load("add.php","user=ввввй&phone=5"); }) }); </script> </head> <body> <p id="par1">После нажатия на кнопку в данном поле будет отображен результат выполнения скрипта add.php.</p> <input id="edit1" type="Edit" value="имя" /> <input id="edit2" type="Edit" value="телефон" /> <input id="but1" type="button" value="Передать данные скрипту" /> </body> </html> 

 <?php header ("Content-Type: text/html; charset=utf-8"); // подключаемся к серверу баз данных MySQL $db = new mysqli("localhost", "root", ""); // устанавливаем кодировку общения с MySQL $db->query("SET NAMES 'utf8'"); $db->select_db("myDataBase111"); $user = $_GET['user']; $phone = $_GET['phone']; echo "Имя: $name, Фамилия: $surname"; $db->query("INSERT INTO phones (fio, phone) VALUES ('$user','$phone')"); ?> 

This data is sent and saved. But how can I get the values ​​from edit1/edit2 and pass them on?
thank

  • one
    do POST or .serialize () - zb '
  • 2
    Add the name attribute to your input with the name and phone values, respectively. After that use the advice @eicto - fremail
  • For starters, it would be nice to put these inputs into the form ... - user6550
  • one
    See what magic> $ db-> query ("INSERT INTO phones (fio, phone) VALUES ('$ user', '$ phone')"); I am an evil hakir. I send a request that expands to the following variable: $ user = 'fio \', \ '+ 7 929 999 99 99 \'); DROP TABLE phones; “And the phones table is gone.” - etki
  • Thank you)), I am analyzing the simplest example, then I will do it with parameters - Rakzin Roman

1 answer 1

Thank you very much. Understood.

 <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#but1").click(function(){ $.post ('add1.php',{login:$("#edit1").val(),password:'password'},function(data,status){ if( status=='success' ){ eval('var obj='+data); if(obj.auth==true) { alert(obj.otvet); } else alert('Логин либо пароль не верны') }else{ alert('В процессе отправки произошла ошибка :(') } }) }); }); </script> </head> <body> <div id="div1"> </div> <input id="edit1" type="Edit" value="Передать имя" /> <input id="but1" type="button" value="Передать данные скрипту" /> </body> </html> ---------- add1.php <?php if($_POST['login']=='1' and $_POST['password']=='password'){ $auth = true; $User=$_POST['login']; $otvet = "Я принял твой запрос это $User"; // setcookie('auth',md5($password.$login),time()+3600,'/'); }else $auth = false; die( json_encode(array('auth'=>$auth,'otvet'=>$otvet)) ); ?>