Good evening, please tell me what you need to register in the add function to pass the test data array?

The form:

<form action="" method="post" onsubmit="add();return false;"> Тест поле1: <input type="text" value="..." name="test[name]" /><br/> Тест поле2: <input type="text" value="5" name="test[price]" /><br/> <input type="submit" value="Добавить" /> </form> 
  • one
    @infolabs, According to the rules of the forum, questions should not be limited to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - Sergiks
  • I haven’t done anything, because I don’t understand how to pass a test to a function. So I ask. And what is the educational task, why did you think that I somehow belong to an educational institution? - infolabs
  • Synchronously or asynchronously? - dlarchikov
  • Better of course asynchronously. But it does not matter. - infolabs
  • one
    about jquery heard? This is the easiest way. - dlarchikov

1 answer 1

You need to connect jquery, in the form instead of submit, specify: <input type = "button" onclick = "add ()" value = "Add" />

Js file

 function add() { var name = $("#name").val(); var price = $("#price").val(); $.ajax({ type:"POST", data: {"name":name ,"price":price}, url:"script.php", success:function(html){ alert(html); } }); } 

And in script.php you need to remember to check the session and cookies so that no one adds data by contacting this script directly.

  • one
    never use onclick in html markup with jquery. All the more so for forms you must use submit and not click. - zb '
  • "never use onclick in html markup with jquery" Why? - Zepp
  • You probably did not understand the question. The question was quite different. Separately, I know perfectly how to transfer data - infolabs
  • @Zepp stackoverflow.com/questions/12627443/jquery-click-vs-onclick I can add to what was said in the link that in this way you are also forced to litter the global scope with functions that you have nothing to do in the global. - zb '