douxuanma4357 2017-03-21 11:58
浏览 27
已采纳

从Javascript插入的值的PHP比较

I send values from php to script

<img src=\"images/add.jpg\" onclick='add_program_user(".$value['id_program'].",".$value['min_age'].",".$value['max_age'].")' onmouseover=\"this.style.cursor='pointer'\" />     

The script is:

function add_program_user(id_program){
    var str="./add_program_user.php?p1="+id_program+"&p2="+min_age+"&p3="+max_age;
    window.location=str;    
}

What I would like to do is check in add_program_user.php if the user has the correct age range. My not working code is:

$query = "SELECT age FROM user WHERE user.mail = '".$_SESSION['logged_user_mail']."'";
    $res = @mysqli_query($con,$select_query) or die('Error, query1 failed');
    $num_res = mysqli_num_rows($res);
    if($age< min_age || $age> max_age){
        echo '<html><meta charset="UTF-8"><script language="javascript">alert("Wrong age range."); document.location="user_programs.php";</script></html>';
    }

Any help? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douzengjian1535 2017-03-21 12:26
    关注

    I'd like to suggest some changes to the image section. This just simply makes it easier to read (IMO).
    What I have done is to just wrap the array values in curly braces ({}) which means that you don't have to concatenate the string with the full stop, which I find easier to read. Note that it is only available when using the double quotes ".
    So what that means is add_program_user(".$value['id_program']."," becomes add_program_user({$value['id_program']},

    echo "<img src=\"images/add.jpg\" onclick=\"add_program_user({$value['id_program']}, {$value['min_age']}, {$value['max_age']})\" onmouseover=\"this.style.cursor='pointer'\" />";
    

    My second note would be regarding your JavaScript function. As Patrick Manser said in the comments, you were only passing one argument to the function, but trying to get 3 from it. This is easily rectified by changing the function to the following.

    function add_program_user(id_program, min_age, max_age) {
        var str = "./add_program_user.php?p1=" + id_program + "&p2=" + min_age + "&p3=" + max_age;
        window.location = str;
    }
    

    Finally, you didn't use the $ for the variable names (i.e. $min_age in the if statement).
    So, I propose the following edits to the PHP script.
    I'd also suggest using prepared queries in your future queries.

    <?php
    // store the get variables
    $id_program = $_GET["p1"];
    $min_age    = $_GET["p2"];
    $max_age    = $_GET["p3"];
    
    $query = "SELECT age FROM user WHERE user.mail = '{$_SESSION['logged_user_mail']}'";
    $res = @mysqli_query($con, $query) or die('Error, query1 failed');
    $num_res = mysqli_num_rows($res);
    
    // ensure only one user is selected
    if ($num_res == 1)
    {
        $age = mysqli_fetch_array($res, MYSQLI_ASSOC); // store the data
        // check the age range
        if ($age < $min_age || $age > $max_age)
            echo '<html><meta charset="UTF-8"><script language="javascript">alert("Wrong age range."); document.location="user_programs.php";</script></html>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False