doulin8374 2014-07-22 13:42
浏览 67
已采纳

php中的javascript警告框显示为网页上的实际文本:alert <x >>>

I'm trying to make a javascript alert box popup inside some PHP code.

<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. 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

  • 写回答

1条回答 默认 最新

  • dongliuliu0385 2014-07-22 21:03
    关注

    The reason it prints as text is because you are putting the output of the 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

    <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();

    e.g.

    <input type="submit" name="my_submit_button" />
    
    <?PHP
    if (isset($_POST['my_submit_button'])){
        storeMemberData();
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。