比较JavaScript变量和PHP数组

问题描述 投票:0回答:1
<link type="text/css" rel="stylesheet" href="style_downloads.css" />
<script>
function theFunction(e){ 
    var x = e.target.innerHTML;
    document.write(x);
}
</script> 
<a onclick="theFunction(event)" href="">Electromagnetic Theory</a> 
<a onclick="theFunction(event)" href="">Manufacturing Technology-I</a>
<?php
    $con = mysqli_connect('localhost', 'root');
    if (!$con) echo "Connection Failed";
    mysqli_select_db($con, 'test') or die(mysqli_error($con));

    $q      = "SELECT * FROM tb1";
    mysqli_query($con, $q) or die(mysqli_error($con));
    $result = mysqli_query($con, $q);
    $num    = mysqli_num_rows($result);


    for ($i = 1; $i <= $num; $i++) {   
        $row = mysqli_fetch_array($result); 
        if(?>
        <script>x</script>
        <?php==$row['coursename'])
        { ?>
         <div id="downloads"> <span id="pdfname"><?php echo $row['pdfname'] ?></span> <a href="upload/<?php echo $row['file'] ?>"> <img src="download.png" id="downloadbtn"></a> </div>
        <?php
        }
    }
    mysqli_close($con);
?>

[这里,我想将js的var x与$ row ['coursename']进行比较,即mysql db表条目。基本上,此代码的目标是根据onclick事件显示数据库表条目。例:当我单击“电磁理论”时,页面上显示的文件应仅与此课程名称相对应。

javascript php mysql
1个回答
-1
投票
<link type="text/css" rel="stylesheet" href="style_downloads.css" />

    <script>
    function theFunction(e)
    { 
        var itemName =  e.target.innerHTML;

            $.ajax({
                url     : "/ajaxAction.php",
                type    : "POST",
                data    : {'itemName':itemName},
                dataType: 'json',
                success: function (response) { 
                console.log("response",response)        
                    if(response.type == "success") {
                        $("#downloads").html('');
                        $("#downloads").html(response.result);                     
                        return false;
                    }
                }
            });
    }
    </script>

<a onclick="theFunction(event)" href="">Electromagnetic Theory</a>
<a onclick="theFunction(event)" href="">Manufacturing Technology-I</a>

<div id="downloads">
    <span id="pdfname"><?php echo $row['pdfname'] ?></span>
    <a href="upload/<?php echo $row['file'] ?>"> <img src="download.png" id="downloadbtn"></a>
</div>

    <?php
    create file ajaxAction.php
    incluede connection.php

    $itemName   = $_POST['itemName'];
    $con        = mysqli_connect('localhost', 'root');
    if (!$con)
        echo "Connection Failed";

    mysqli_select_db($con, 'test') or die(mysqli_error($con));
    $q      = "SELECT * FROM tb1";
    mysqli_query($con, $q) or die(mysqli_error($con));
    $result = mysqli_query($con, $q);
    $num    = mysqli_num_rows($result);
    for ($i = 1; $i <= $num; $i++) {   
        $row = mysqli_fetch_array($result); 
        if($itemName == $row['coursename'])
        { 
            $res = ' <span id="pdfname"><?php echo $row['pdfname'] ?></span>
                <a href="upload/<?php echo $row['file'] ?>"> <img src="download.png" id="downloadbtn"></a>';
        }
     }
    echo json_encode("type":"success","result":$res);
    mysqli_close($con);
    ?>
© www.soinside.com 2019 - 2024. All rights reserved.