The problem is that the value of "m_id" is not being passed to the PHP script "write_temp/Table.php" from the AJAX request.
In the PHP script, you are trying to access the "m_id" value using the $_POST["m_id"] variable, but it seems that the value is not being passed in the data of the AJAX request.
You need to change your ajax data to
data: {m_id : m_id},
Also, you can check if you are getting m_id in your script by adding this line
echo $_POST["m_id"];
on top of your script just after the <?php to check if it is getting the value or not.
It is also a good practice to add error handling on your php script.
Try this:
Code (PHP)
if(isset($_POST["m_id"])){
$query = "SELECT * FROM intrument_registor WHERE m_id = '".$_POST["m_id"]."'";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)){
$Database_name = $row["Database_name"];
}
}
else {
echo "m_id is not set.";
}
It will display the message if the m_id is not set in the post request
และหลังจากขอตัวอย่างการใช้ AJAX นี่คือสิ่งที่ได้
Here's an example of how you might use jQuery's .ajax() method to make a POST request to a PHP script that uses prepared statements to interact with a MySQL database:
JavaScript/jQuery:
$(document).on('click', '.submit-button', function(){
var name = $("#name").val();
var email = $("#email").val();
var phone = $("#phone").val();
$.ajax({
url: "submit.php",
method: "POST",
data: {name: name, email: email, phone: phone},
success: function(data){
alert(data);
}
});
});
In the JavaScript, we are listening to the click event of a submit-button. When the button is clicked, we are getting the value of name, email, phone and then making an AJAX request to the "submit.php" file. The data object is passed to the server-side script, which contains the values of the name, email, and phone.
In the PHP script, we are first creating a connection to the MySQL server. After that, we are getting the value of name, email, phone from the $_POST variable.
We are creating a prepared statement which will insert the values in the users table of a myDB database. Then we are binding the variable to the prepared statement and executing it.
After the execution of the statement, we are sending the message of success via echo. And we are closing the statement and connection.
It is always a good practice to use prepared statement when interacting with a MySQL database to prevent SQL injection attacks.