Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ผมมือใหม่งงกับ jQuery AJAX มาหลายวันว่าทำไม POST ค่า ID ไปฟอร์มอีกฟอร์มไม่ได้ครับ



 

ผมมือใหม่งงกับ jQuery AJAX มาหลายวันว่าทำไม POST ค่า ID ไปฟอร์มอีกฟอร์มไม่ได้ครับ

 



Topic : 136921



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์




งงมากครับว่าต้องการ post ค่า m_id ไปไฟล์ Table.php แต่ทำไมทำไม่ได้ครับ
นี่ตัวอย่างเมื่อกดปุ่มส่งให้ post ค่า และเปิดหน้าใหม่ครับ

$(document).on('click', '.table_data', function(){ ///ค้นหาข้อมูลแสดงในตารางตามช่วงเวลา
var m_id = $(this).attr("m_id");
if(m_id =='') {
alert("ไม่สามารถระบุตำแหน่งข้อมูลที่ทำการเลือกได้");
return false;
}
else
{
$.ajax({
url: "write_temp/Table.php",
method:"POST",
data:{m_id : m_id},
success:function(data){
//console.log('success',data);
//window.location.replace('write_temp/table/table_index.php'); //เปิดฟอร์ม///////////////////
//swindow.open('write_temp/table/table_index.php'); //เปิดฟอร์ม///////////////////
alert(m_id);
location.href = "write_temp/table/table_index.php"; // Ajax redirect

}
});
}
});


ส่วนอันนี้คือโค้ดที่จะรับค่า post ของ m_id ครับ

<?php
//ดึงข้อมูลใส่ในตัวแปล ID ที่เลือกจากฟอร์ม "บันทึกอุณหภูมิเครื่องมือ"
$Database_name ="";

$connect = mysqli_connect("localhost", "root", "43310937", "instrument_history");

//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"];
}
//}
?>



Tag : PHP, MySQL







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2023-01-10 22:54:35 By : chainarong.k View : 528 Reply : 3
 

 
คำตอบจาก AI (ลองทำตามดู)




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.

PHP(submit.php)
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];

$stmt = $conn->prepare("INSERT INTO Users (name, email, phone) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $phone);
$stmt->execute();

echo "New record created successfully";

$stmt->close();
$conn->close();
?>




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.











แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-11 00:28:19 By : 009
 


 
$(this).attr("id"); //โบราณ
หรือ
$(this).prop("id");
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-11 08:43:50 By : Guest
 

 

No. 3



โพสกระทู้ ( 9,556 )
บทความ ( 2 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 2 เขียนโดย : Guest เมื่อวันที่ 2023-01-11 08:43:50
รายละเอียดของการตอบ ::
dom: this.id
วิธีการอ้างอิงดั้งเดิมของ javascript ส่วน attr กับ prop ใช้กับ attribute อื่นๆ ที่ javascript ไม่ได้ support ไว้ให้


attr จะใช้ได้ดี กับ attribute ที่มี = "value"
prop ใช้ได้ดีกับ attribute ที่ไม่มี = "value" มีแต่ชื่อเฉยๆ

ไม่มีอะไร โบราณ ทำงานได้เป็นใช้ได้
ผลลัพธ์สำคัญกว่าวิธีการ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-12 08:01:55 By : Chaidhanan
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ผมมือใหม่งงกับ jQuery AJAX มาหลายวันว่าทำไม POST ค่า ID ไปฟอร์มอีกฟอร์มไม่ได้ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่