简体   繁体   English

如何将值传递给div? Jquery

[英]How to pass values to a div? Jquery

I have a form below, to pass below values to div (myDiv) and want that div to be shown (display:block).我在下面有一个表格,用于将以下值传递给 div (myDiv) 并希望显示该 div (display:block)。

<form method="post">

       <input type="text" name="value_a" value="A" >
    <input type="text" name="value_b" value="B" >

<input type="submit" name="submitX" value="submit" ;">

</form>


<div id="myDiv" style="display: none;"  >

<?php if(isset($_POST['submitX'])) 
  
{

  $AA= $_POST['value_a'];
  $BB = $_POST['value_b'];  
  .
  .
</div>

I already try below input type:我已经尝试了以下输入类型:

<input type="button" name="submitX" value="Submit" onclick="document.getElementById('myDiv').style.display = 'block' ;" >

It shows the div(myDiv) but the values i wanted not carry along.它显示了 div(myDiv) 但我不想携带的值。

Thanx:)谢谢:)

You assign values to variables, but you never actually output them.你给变量赋值,但实际上你从来没有output他们。 Instead of this:而不是这个:

$AA= $_POST['value_a'];

Simply do this:只需这样做:

echo $_POST['value_a'];

Additionally, your <div> should probably be visible in order to see it.此外,您的<div>应该是可见的才能看到它。 This is styled to always be hidden:这被设计成总是隐藏的:

<div id="myDiv" style="display: none;"  >

Even if you run JavaScript to show it when clicking the button, that won't matter as soon as the form posts and the page reloads.即使您在单击按钮时运行 JavaScript 以显示它,一旦表单发布和页面重新加载,这也无关紧要。 Instead, conditionally style it based on the form input:相反,根据表单输入有条件地设置样式:

<div id="myDiv" style="display: <?= isset($_POST['submitX']) ? "block" : "none" ?>;"  >

Or if you prefer for readability:或者,如果您更喜欢可读性:

<?php if (isset($_POST['submitX'])) { ?>
  <div id="myDiv">
<?php } else { ?>
  <div id="myDiv" style="display: none;">
<?php } ?>

There are a variety of ways to structure your logic, really.实际上,有多种方法可以构建您的逻辑。


As a learning exercise, further considerations include:作为学习练习,进一步的考虑包括:

  1. Formatting your output so the values aren't just munged together in the result.格式化您的 output,以便结果中的值不只是混在一起。 This would involve using more HTML in your output. Observe the browser's page source to see what you actually output vs. how it's rendered.这将涉及在您的 output 中使用更多 HTML。观察浏览器的页面源代码以了解您实际 output 与它的呈现方式。
  2. Start looking into something called Cross Site Scripting .开始研究称为Cross Site Scripting的东西。 For a contrived exercise like this one, it's not a problem.对于像这样的人为练习,这不是问题。 But in real-world applications you don't want to blindly output user input, especially if you're outputting someone else's input to the user.但是在现实世界的应用程序中,您不想盲目地 output 用户输入,尤其是当您正在将其他人的输入输出给用户时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM