R
R
riper98192017-06-07 13:20:44
PHP
riper9819, 2017-06-07 13:20:44

How to execute SQL query on button click?

Hello, I'm new to web development and I need a SQL query to be executed when a button is clicked. Is it possible to do this through Ajax, and is it safe at all (I read somewhere that data can be stolen from the database in this way). Or maybe there is an easier way? If it needs to be done via Ajax anyway, can you explain to me how to do it please.
Here is the button code and the SQL query itself:

<?php
echo '<input type="Submit" name="sendMessage"  value="Написати продавцю" onclick="window.location.href=\'send_message.php?itemid='.$row['idItem'] .'\'; return false;" >';


if (isset($_POST['sendMessage']))
{
$_SESSION['sellerEmail'] = $row['itemSellerEmail'];
}
?>


Thank you in advance for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-06-07
@riper9819

Is it possible to do this through Ajax, and is it safe at all (I read somewhere that data can be stolen from the database in this way).

Security is not so much a matter of Ajax as of how you process the request itself.
Or maybe there is an easier way?
It all depends on your task, in the general case you need to send a request to the Backend and execute the necessary database query there. If you need to send a request without reloading the page - then yes, you need to use Ajax
If it needs to be done via Ajax anyway, can you explain to me how to do it please.
The Internet is full of information on how to work with Ajax, so it's better to google and find the information that suits you. As an option, you can consider from this

S
Sergey Sobko, 2017-06-07
@Catzo0

I wrote to you yesterday, I will write to you today.
There is no SQL query in the above code.
To submit form data to the handler file, you need to
1) Wrap your input in a form tag.
2) Specify the necessary attributes for the form tag: action="the file you need.php" action="POST". For other attributes of the form tag, information should be taken from the official HTML documentation from w3c.
3) Add a button with the Submit type to the form.
Also learn the difference between POST and GET.
Don't touch AJAX yet - you still need to grow up to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question