PHP / HTML:使用php创建一个元素,只需单击一个按钮即可

PHP / HTML:使用php创建一个元素,只需单击一个按钮即可

问题描述:

I've been trying to do something very complicated (in my opinion, because it doesn't quite work the way I want it to). What I want, is that I have a text field where I type in something, then next to that is a button, and when you type in something in the text field, then press the button a new html element will appear (a box with in there the text you typed in the text field). Now, I've been trying this many different ways. Here's the code I'm using now:

<html>

<head>

<style>
div.cat {
width: 200px;
height: 25px;
border: 1px solid black;
}
</style>

</head>

<?php

$form = "<form method=\"post\" action=\"<?php echo $_SERVER['PHP_SELF']; ?>\"><input type=\"text\" name=\"name\"><br><input type=\"submit\" value=\"Submit\"></form>";

function newCategory() {
$categoryname = $_POST['name'];
$category = "<div class=\"cat\"> <?php $categoryname ?> </div>"

echo $category;
}


?>

<body>

<?php
echo $form;

newCategory();
?>

</body>

</html>

Now, the issue seems to be that if you press the button, the newCategory() function won't execute again. Please help!

EDIT: The issue was in the syntax errors. It's still not working the way I want it to work, though I now know what went wrong. I guess I have to use JavaScript.

我一直在尝试做一些非常复杂的事情(在我看来,因为它不是很有效 我希望它)。 我想要的是,我有一个文本字段,我输入内容,然后旁边是一个按钮,当你在文本字段中键入内容时,然后按下按钮将出现一个新的html元素(一个包含 在那里你在文本字段中输入的文本)。 现在,我一直在尝试这么多不同的方式。 这是我现在使用的代码: p>

 &lt; html&gt; 
 
&lt; head&gt; 
 
&lt; style&gt; 
div.cat {
width:200px  ; 
height:25px; 
border:1px纯黑; 
} 
&lt; / style&gt; 
 
&lt; / head&gt; 
 
&lt;?php 
 
 $ form =“&lt; form method =  \“post \”action = \“&lt;?php echo $ _SERVER ['PHP_SELF'];  ?&gt; \“&gt;&lt; input type = \”text \“name = \”name \“&gt;&lt; br&gt;&lt; input type = \”submit \“value = \”Submit \“&gt;&lt;  ; / form&gt;“; 
 
函数newCategory(){
 $ categoryname = $ _POST ['name']; 
 $ category =”&lt; div class = \“cat \”&gt;&lt;?php $  categoryname?&gt;&lt; / div&gt;“
 
echo $ category; 
} 
 
 
?n&gt; 
 
&lt; body&gt; 
 
&lt;?php 
echo $ form; 
  
newCategory(); 
?&gt; 
 
&lt; / body&gt; 
 
&lt; / html&gt; 
  code>  pre> 
 
 

现在,问题似乎是 如果按下按钮, newCategory() code>函数将不再执行。 请帮助! p>

编辑:问题在于语法错误。 尽管我现在知道出了什么问题,但它仍然没有像我希望的那样工作。 我想我必须使用JavaScript。 p> div>

try this code...

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<input type="text" id="text1"/>
<input type="button" id="btn" value="Button"/>
<div id="div1">
</div>
<script>
$('#btn').click(function(){
var gettext=document.getElementById("text1").value;
var temp=document.createElement("input");
temp.id="text2";
temp.value=gettext;
$("#div1").html(temp);
});
</script>

The problem is in this line of code: $category = "<div class=\"cat\"> <?php $categoryname ?> </div>". You cannot have php tags <?php ?> inside another set of php tags.

Just change it to this: $category = "<div class=\"cat\">".$categoryname."</div>";.

And make sure you fix all the other syntax errors as well.

You have syntax error, Replace this to your whole code with this, it is working well

<html>
<head>
<style>
div.cat {
    width: 200px;
    height: 25px;
    border: 1px solid black;
}
</style>
</head>
<?php

    $form = "<form method='post' action='".$_SERVER['PHP_SELF']."'><input type='text' name='name'><br><input type='submit' value='Submit'></form>";

    function newCategory() {
        $categoryname = $_POST['name'];
        $category = "<div class='cat'>".$categoryname."</div>";

        return $category;
    }

?>
<body>
<?php
        echo $form;
        echo newCategory();
    ?>
</body>
</html>