如何在HTML PHP中传递字符串

时间:2013-07-11 00:26:12

标签: php html text onclick

#!/usr/local/bin/php

<!DOCTYPE html>
<html>
<head>
<script>
function sendInfoToChart()
{
document.getElementById("firstN").innerHTML=document.getElementById("firstName").innerHTML;
}
</script>
</head>
<body>

<form action="">
First name: <input type="text" name="firstName" id="firstName"><br>

<button onclick="sendInfoToChart()"> Submit </button>
</form>
<br><br>
<table border="1" cellpadding="4" style="font-style:italic;">
        <tr><td><p id="firstN"></p></td><td>MiddleName</td><td>Last Name</td></tr>
        </body>
</html>

我正在尝试传递文本字段中键入的单词,并在单击按钮时将其传递给表。我无法弄清楚如何传递它...提前感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

您必须使用文本框的`value属性来获取其文本并阻止表单提交以查看更改。

document.getElementById("firstN").innerHTML=document.getElementById("firstName").value;
...
<button onclick="sendInfoToChart();return false;"> Submit </button>

http://jsfiddle.net/jGvZz/

答案 1 :(得分:1)

您可以将输入存储在变量中,然后再使用它。请注意,我在这里使用了value,而不是innerhtml

var firstname = document.getElementById("firstName").value;

document.getElementById("firstN").innerHTML= firstname

答案 2 :(得分:0)

You can do this using simple PHP code    
<!DOCTYPE html>
    <html>
        <head>

        </head>
        <body>

            <form action="" method="post">
                First name: <input type="text" name="firstName" id="firstName"><br>

                <input type="submit" name="submit" value="Submit"/>
            </form>
            <br><br>
            <table border="1" cellpadding="4" style="font-style:italic;">\
                <tr><td><p id="firstN">
                        <?php 
                        if (isset($_POST['submit'])) {
                                echo $_POST['firstName'];
                        } 
                        ?>
                        </p></td><td>MiddleName</td><td>Last Name</td></tr>
            </table>        
        </body>
    </html>