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>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳