<body>
<form id='form'>
<select id='name' name='selectName'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
<select id='age' name='selectAge'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
<input type='submit' value='submit'>
</form>
<div id='print'></div> <!-- Here you will print the submitted values -->
</body>
</html>
<!-- ES6 syntax -->
<script>
const form = document.getElementById('form');
const print = document.getElementById('print');
form.addEventListener('submit', function(event) {
event.preventDefault(); // prevent page reload
const name = this.querySelector('#name').value; // get the name
const age = this.querySelector('#age').value; // get the age
print.innerHTML += `<div>Name: ${name}, Age: ${age}</div>`; // print name and age below the form
// here you can perform an AJAX call to your PHP file and do something with it
});
</script>
在這種情況下,沒有理由把action='YOUR_PHP_FILE.php'
形式,因爲要保持頁面和下面的印刷信息,所以只進行幕後AJAX調用。通常你會用:
<form id='form' action='YOUR_PHP_FILE.php' method='POST'>
// ...
</form>
在php
文件,你可以這樣做:
<?php
$name = $_POST['selectName'];
$age = $_POST['selectAge'];
// do something with these values ...
?>
這裏是老版本的Javascript:
<!-- Old syntax -->
<script>
var form = document.getElementById('form');
var print = document.getElementById('print');
form.addEventListener('submit', function(event) {
event.preventDefault(); // prevent page reload
var name = this.querySelector('#name').value; // get the name
var age = this.querySelector('#age').value; // get the age
print.innerHTML += '<div>Name: ' + name + ', Age: ' + age + '</div>'; // print name and age below the form
// here you can perform an AJAX call to your PHP file and something with it
});
</script>
1.是的,它是可能的。 2.你需要給你的'
謝謝你的迴應。是$ variable = $ _POST ['name'];其中名稱是
是的,當你使用'method =「post」'它是'$ _POST [:]':-) – andlrc