งานนี้สมบูรณ์แบบ! ;)
สิ่งนี้สามารถทำได้โดยใช้ Ajax และด้วยสิ่งที่ฉันเรียกว่า: "องค์ประกอบกระจกแบบฟอร์ม" แทนที่จะส่งแบบฟอร์มพร้อมองค์ประกอบภายนอกคุณสามารถสร้างแบบฟอร์มปลอม ไม่จำเป็นต้องใช้แบบฟอร์มก่อนหน้า
<!-- This will do the trick -->
<div >
<input id="mirror_element" type="text" name="your_input_name">
<input type="button" value="Send Form">
</div>
โค้ดอาแจ็กซ์จะเป็นอย่างไร:
<script>
ajax_form_mirror("#mirror_element", "your_file.php", "#your_element_response", "POST");
function ajax_form_mirror(form, file, element, method) {
$(document).ready(function() {
// Ajax
$(form).change(function() { // catch the forms submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: method, // GET or POST
url: file, // the file to call
success: function (response) { // on success..
$(element).html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
});
}
</script>
นี่เป็นประโยชน์อย่างมากหากคุณต้องการส่งข้อมูลบางอย่างภายในแบบฟอร์มอื่นโดยไม่ต้องส่งแบบฟอร์มหลัก
รหัสนี้อาจจะสามารถปรับ / ปรับให้เหมาะสมตามความต้องการ มันทำงานได้อย่างสมบูรณ์แบบ !! ;) ใช้งานได้หากคุณต้องการกล่องตัวเลือกที่เลือกดังนี้:
<div>
<select id="mirror_element" name="your_input_name">
<option id="1" value="1">A</option>
<option id="2" value="2">B</option>
<option id="3" value="3">C</option>
<option id="4" value="4">D</option>
</select>
</div>
ฉันหวังว่ามันจะช่วยคนอย่างที่มันช่วยฉัน ;)