[英]Second button doesn't produce form in same div as first button
I have a php page with two divs.我有一个带有两个 div 的 php 页面。 In the first div, users click a button and it brings up a form in the second div.
在第一个 div 中,用户单击一个按钮,它会在第二个 div 中显示一个表单。 When they submit the form, both divs refresh.
当他们提交表单时,两个 div 都会刷新。 All is great.
一切都很好。 But I want to add a second button to the first div to bring up a different form in the second div.
但我想在第一个 div 中添加第二个按钮,以在第二个 div 中显示不同的表单。 I haven't been able to get this to work, either the first button stops working or the second one doesn't work.
我无法让它工作,要么第一个按钮停止工作,要么第二个按钮不起作用。 I tried making the second button a div and calling it by its ID and that didn't work.
我尝试将第二个按钮设为 div 并通过其 ID 调用它,但没有成功。 I'm an amateur and don't understand jquery very well (I think this is jquery anyway) and this is just for a project for some friends and I, so any help would be appreciated.
我是一个业余爱好者,不太了解 jquery (我认为这是 jquery 无论如何),这只是为一些朋友和我的项目,所以任何帮助将不胜感激。 I may just not be understanding how this works.
我可能只是不明白这是如何工作的。
<script>
$(document).ready(function () {
$("button").click(function () {
var handle = "<?php echo $_SESSION['name'] ?>";
$("#infoblock").load("createhandle.html");
});
});
</script>
<div id="div1">
<button id="reservename">Reserve A Name</button>
</div>
<div id="div2"></div>
I found a solution here: https://stackoverflow.com/a/20074373/9157720我在这里找到了解决方案: https://stackoverflow.com/a/20074373/9157720
It ended up being an issue with syntax and using a pound symbol.它最终成为语法问题并使用井号。 This is the code that worked in case anyone else has this problem.
这是在其他人遇到此问题时有效的代码。
<script>
$(document).ready(function(){
var handle = "<?php echo $_SESSION['name'] ?>";
$("#reservename").click(function(){
$("#infoblock").load( "createhandle.html");
});
$("#uploadpic").click(function(){
$("#infoblock").load("uploadpic.html");
});
});
</script>
<button id="reservename">Reserve A Name</button>
<button id="uploadpic">Upload A Picture</button>
Consider the following.考虑以下。
<script>
$(document).ready(function () {
$("button").click(function () {
var handle = $(this).data("ref");
var target = $(this).data("target");
$(target).load("createhandle.html?handle=" + handle);
});
});
</script>
<div id="div1">
<button data-ref="<?php echo $_SESSION['nameA']; ?>" data-target="#infoblock">Reserve A Name</button>
<button data-ref="<?php echo $_SESSION['nameB']; ?>" data-target="#div2">Reserve B Name</button>
</div>
<div id="div2">
</div>
Your example is sort of ambiguous, so I wasn't sure what you were trying to accomplish.您的示例有点模棱两可,所以我不确定您要完成什么。 It's best to not mix PHP and HTML if you can.
如果可以的话,最好不要混合 PHP 和 HTML。 You can have a reference point in the HTML so that when you need to load something, in relationship to that button, you can pass that into a stand alone PHP Script and get the HTML you need back with
.load()
.您可以在 HTML 中有一个参考点,这样当您需要加载与该按钮相关的某些内容时,您可以将其传递到独立的 PHP 脚本并获取 Z4C4AD5FCA2E7A3F74DBB1CED00381AA.4Z 您需要的回载
.load()
Remember that PHP is executed before the HTML is presented to the Client.请记住,PHP 在 HTML 呈现给客户端之前执行。 So the Session data must be present in PHP Memory when the page is initially loaded.
因此,最初加载页面时,Session 数据必须存在于 PHP Memory 中。
Reference:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.