简体   繁体   English

单击按钮时如何显示文本字段?

[英]How to show textfields when we click a button?

I am trying to make a form which will show only one text field and a button when its load, but when we click that button it will show one more text field and a button under it and when we click that button it will show three text field and one button and this latest button will submit the form....is it possible?.....if it then please guide me....this is my code... 我正在尝试制作一个仅在加载时仅显示一个文本字段和一个按钮的表单,但是当我们单击该按钮时,它将显示另一个文本字段和一个按钮,当我们单击该按钮时,它将显示三个文本字段和一个按钮,最后一个按钮将提交表单....可以吗?.....如果可以,请指导我....这是我的代码...

<div id="envelope">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<center><h1>Edit an existing generic drug</h1></center><br>
<label>Generic Drug Name</label>
<select name = "position" type = "option">
<?php 
$query = "SELECT * FROM brand_generic.generic_drug WHERE 1";
$result =  mysqli_query($connection, $query);
if(!$result){
    die("Database query failed.");
}?><?php
while ($row =  mysqli_fetch_assoc($result)) {?>
     <option value = "<?php echo $row['generic_drug_name'] ?>"
     <?php echo((isset($_POST['position']) && $_POST['position'] == $row['generic_drug_name']) ? 'selected="selected"' : ' ');?>
     ><?php echo $row['generic_drug_name'];?></option>
<?php }

?>
</select>
<input type="submit" name = "edit" value="Edit" id="submit"/>
<?php
    if(isset($_POST['edit'])){ ?>
        <label>New Name</label>
        <input type="text" name="drug_name" value="<?php echo $_POST['position']; ?>" width="100px;"/>
        <input type="submit" name = "submit" value="Submit" id="submit"/>
   <?php 
      if(isset($_POST['submit'])){
        $edited_drug_name = $_POST['drug_name'];
        echo "Hello world";         
      } 
   }

?>  
</form>
</div>

it works when I click on first button it shows another text field and button but when I click new button it doesn't echo out the message...please help 当我单击第一个按钮时它会显示另一个文本字段和按钮,但是当我单击新按钮时它不会回显该消息...请帮助

jquery .append should help. jQuery .append应该会有所帮助。

heres an example. 这是一个例子。

    <script type="text/javascript">
        $( function() {
            $("#button").click( function(){

                var textarea = $("<textarea></textarea>").prop({
                    name: 'ExampleName'
                    style: ''
                }).addClass( 'classesHere' );

                $("form").append(textarea);
            })
        })
    </script>

I would start with a basic form that had one textarea. 我将从具有一个文本区域的基本表单开始。 Remember, ID's cannot start with a number so, you might have to come up with a creative naming convention. 请记住,ID不能以数字开头,因此,您可能必须提出创造性的命名约定。

<form id="myform" action="foo" method="post">
<fieldset>
    <p><label for="comments0">Comments</label>
    <textarea id="comments0" name="comments" cols="11" rows="4"></textarea></p>
    <p><a id="addItem" href="#">+ Add another item</a></p>
</fieldset>
<fieldset>
    <input type="submit" value="Submit" />
</fieldset>

You could dynamically create the HTML using jQuery: 您可以使用jQuery动态创建HTML:

var i = 1;
$('#addItem').click(function(event) {
    var markup = '<p><label for="comments' + i + '">Comments<label><textarea id="comments' + i + '" name="comments" cols="11" rows="4" /></textarea></p>';

    $(this).before(markup);
    i++;
    event.preventDefault();
});

Then in your php file, you could do something like this: 然后在您的php文件中,您可以执行以下操作:

<?php

for ($i=0; $i<$_POST['comments'].length; $i++) {
    // handle each text-area accordingly.
}

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

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