好了,我完成了第一个成功的SQL查询,在PHP中生成了一个下拉框!(是的,我仍然是一个大新手)
我的下一个问题....
如果我点击选择按钮,我必须创建一个新的页面,或者我可以有第二个下拉框生成在同一页面上基于第一个列表的提交?
I saw this:对类似问题的回答
但我仍然不确定的形式,行动和方法。保持自我?我是否需要/应该使用两份不同的表格?(我还没有准备好处理AJAX,远没有准备好处理Java,所以请保留这些选项的表:))或者我应该只是传递到第二个文件。
如果可以的话,你能解释一下每个表单中使用的表单动作和方法吗?
如果您还没有准备好使用AJAX或Javascript,那么您将需要添加第二个PHP页面,该页面将处理用户从第一个下拉框中选择的内容,并显示相应的第二个下拉框。
如果你是新手,那么我建议你一开始就保持简单!提交表单和显示新页面是最基本的方式。
所以在你的第一页的HTML中你需要…
<form action="second_page.php" method="post">
<select name="selection"> ... your drop-down code goes here ... </select>
<button type="submit">Next</button>
</form>
然后在second_page.php中你需要…
$selection = $_POST['selection']; // The variable $selection holds the user's selection
然后可以根据$selection的值输出第二页
我更喜欢通过Ajax和JQuery来实现,就像这样:
function getClassList(elem)
{
var contentRequests, contentarr; // The variable that makes Ajax possible!
try
{
// Opera 8.0+, Firefox, Safari
contentRequests = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
contentRequests = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
contentRequests = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
contentRequests.onreadystatechange = function()
{
if(contentRequests.readyState == 4&& contentRequests.status==200)
{
document.getElementById(elem).innerHTML = contentRequests.responseText;
}
}
var urltofetch="index.php?methodname=getclasses"
contentRequests.open("GET", urltofetch, true);
contentRequests.send(null);
}
function getStudentList(classelem,elem)
{
var classToFetch = classelem + " option:selected";
var contentRequests, contentarr; // The variable that makes Ajax possible!
try
{
// Opera 8.0+, Firefox, Safari
contentRequests = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
contentRequests = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
contentRequests = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
contentRequests.onreadystatechange = function()
{
if(contentRequests.readyState == 4&& contentRequests.status==200)
{
document.getElementById(elem).innerHTML = contentRequests.responseText;
}
}
var classname = $(classToFetch).text();
alert(classname);
var urltofetch="index.php?methodname=getstudents&cname="+classname
contentRequests.open("GET", urltofetch, true);
contentRequests.send(null);
}