简体   繁体   English

PHP内部的javascript警报框显示为网页上的实际文本:警报 <x> &gt;“

[英]javascript alert box inside php displaying as actual text on webpage : alert <x> >"

I'm trying to make a javascript alert box popup inside some PHP code. 我正在尝试在一些PHP代码中弹出一个JavaScript警报框。

<html>
<body>
<a href=http://localhost/myPage/Index.html><button type=“button” />HOME</button></a>
<br><br>
<form name="SaveNewMember" method="post" >
Add New Member <br><br>
Member ID &nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="ID" value="">
<br>
Member Name <input type="text" name="name" value="">
<br><br>
<input type="reset" style="height:50px; width:80px"  value="CLEAR ">
&nbsp;&nbsp;
<input type="submit" value="ADD" style="height:50px; width:80px" action="<?php  storeMemberData();?>">
</form>
<?php
function storeMemberData()
{
    if((isset($_POST["ID"]))&(isset($_POST["name"])))
    {
        $newID      = $_POST["ID"];
        $newName    = $_POST["name"];

        if((strlen($newID)==5)&(strlen($newName)>=6))
        {
            $con=mysqli_connect("localhost","root","","membersdb");
            mysqli_query($con,"INSERT INTO members 
                        (ID,Name) 
                        VALUES ('$newID','$newName')");
        // Echo result
        echo '<script type="text/javascript"> alert("Successful log"); </script>';
        }
        else
        { 
            echo "<script type=\"text/javascript\">"; 
            echo "alert(\"Invalid please try again.\")"; 
            echo "</script>"; 
        }
    }
}
?>

I have two examples in there. 我有两个例子。 Neither one works. 两者均无效。 The rest of the code is executing OK (via a submit button on a form). 其余代码执行正常(通过表单上的提交按钮)。 What gets printed to the webpage after submitting is literally the text 提交后打印到网页上的实际上是文本

alert("Invalid please try again.")">

Any help appreciated, it must be something stupid and basic I'm doing wrong. 任何帮助表示赞赏,这一定是愚蠢而基本的,我做错了。

EDIT : In trying to keep the question brief I didn't print all my code. 编辑:为了使问题简短,我没有打印所有代码。 That was obviously wrong. 这显然是错误的。 Have updated the above to include the html form data above the php code too. 也更新了以上内容,在php代码上方也包含了html表单数据。 This is the complete code. 这是完整的代码。

And this is what prints on my page when it is run (reputation too low to add images) 这就是页面运行时在页面上打印的内容(声誉太低而无法添加图像)

http://imgur.com/hvmzAJ3 http://imgur.com/hvmzAJ3

The reason it prints as text is because you are putting the output of the storeMemberData(); 它以文本形式打印的原因是因为您要放置storeMemberData();的输出storeMemberData(); inside the button "action" attributes. 按钮“操作”属性内。 First, the PHP code will run on the server before any of the HTML output reach the browser, so your HTML will look like 首先,PHP代码将在任何HTML输出到达浏览器之前在服务器上运行,因此您的HTML看起来像

<input type="submit" value="ADD" style="height:50px; width:80px"
action="<script type="text/javascript">alert("Invalid please try again.")</script>">

which creates the error you see. 这会产生您看到的错误。

First, you'll need to remove the action part out of the button. 首先,您需要从按钮中删除操作部分。 Add a name to your submit button so you can check if the form was submitted. 将名称添加到您的提交按钮,以便您可以检查表单是否已提交。

After that, in your PHP code, check if the submit button was posted and call the storeMemberData(); 之后,在您的PHP代码中,检查是否发布了storeMemberData();按钮并调用storeMemberData();

eg 例如

<input type="submit" name="my_submit_button" />

<?PHP
if (isset($_POST['my_submit_button'])){
    storeMemberData();
}
?>

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

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