按下按钮时运行php(Ajax)

时间:2015-10-26 10:18:55

标签: php sql-server ajax variables

首先,我对我的数据库运行查询,并获取有关特定客户ID的客户的所有信息:

    <?php

include 'connect-database.php';


   $sql = "SELECT * FROM Kunde WHERE kundenr LIKE '".$_GET['kundenr']."' ";
   $stmt = sqlsrv_query( $conn, $sql);
   if( $stmt == false){
       die( print_r(sqlsrv_errors(), tue) );
   }
   echo "<table border='1'>";
   echo "<tr><th>Kundenr</th><th>Fornavn</th><th>Etternavn</th><th>Tlf</th><th>Epost</th><th>Produktnr</th><th>Adresse</th></tr>";



while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH)) 
{
        echo "<tr>";
        echo '<td><a href="kundedisplay.php?kundenr='.$row[0].'">'.$row[0].'</a></td>';
        echo "<td>" . $row[1] . "</td>";
        echo "<td>" . $row[2] . "</td>";
        echo "<td>" . $row[3] . "</td>";
        echo "<td>" . $row[4] . "</td>";
        echo "<td>" . $row[5] . "</td>";
        echo "<td>" . $row[6] . "</td>";
        echo "</tr>";
        echo "</table>";

}      echo "<br>";
        echo "<br>";


echo '<button id="redigerkunde" type="button">Rediger Kunde</button>';

您可以在第一行&#34; <a href="kundedisplay.php?kundenr='.$row[0].'">'.$row[0].'</a>中看到该链接,该链接指向此页面。

我想做的是: 当我按下按钮&#34; redigerkunde&#34; (意思是编辑客户),我想使用ajax运行一个新的php文件。我得到这个与

一起工作
   <script type="text/javascript">
    $("button").on('click', function(){

            $.ajax({
                type: 'POST',
                url: "php/ajax.php",

                success: function(data) {
                    alert(data);
                    $("p").text(data);

                }
            });
   });

</script>

但问题是,如何将正确的customerID发送到此ajax.php页面? $ row [0]包含customerID。

现在在ajax.php中:

echo '"'.$_GET['kundenr'].'"';

希望它通过我的kundedisplay.php页面中的按钮将customerID回显给我选择的人

谢谢!

3 个答案:

答案 0 :(得分:3)

通过ajax使用data

发送变量
$.ajax({
            type: 'POST',
            url: "php/ajax.php",
            data: {key:value,key:value},// this line.
            success: function(data) {
                alert(data);
                $("p").text(data);

            }
        });

您可以发送n个密钥=&gt;价值对。

下一步:

你在ajax中使用post,所以在ajax.php中,

echo $_POST['kundenr'];// no need of so may quotes.

答案 1 :(得分:0)

试试这个。

<?php

include 'connect-database.php';
   $sql = "SELECT * FROM Kunde WHERE kundenr LIKE '".$_GET['kundenr']."' ";
   $stmt = sqlsrv_query( $conn, $sql);
   if( $stmt == false){
       die( print_r(sqlsrv_errors(), tue) );
   }
   echo "<table border='1'>";
   echo "<tr><th>Kundenr</th><th>Fornavn</th><th>Etternavn</th><th>Tlf</th><th>Epost</th><th>Produktnr</th><th>Adresse</th></tr>";



while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH)) 
{
        echo "<tr>";
        echo '<td><a href="kundedisplay.php?kundenr='.$row[0].'">'.$row[0].'</a></td>';
        echo "<td>" . $row[1] . "</td>";
        echo "<td>" . $row[2] . "</td>";
        echo "<td>" . $row[3] . "</td>";
        echo "<td>" . $row[4] . "</td>";
        echo "<td>" . $row[5] . "</td>";
        echo "<td>" . $row[6] . "</td>";
       echo '<button class="redigerkunde" data-id="'.$row[0].'" type="button">Rediger Kunde</button>';
       echo "</tr>";
}
echo "<table>";

在Java Script中,您可以输入:

<script type="text/javascript">
    $("button").on('click', function(){
            $.ajax({
                type: 'POST',
                url: "php/ajax.php",
                data: {id:$(this).attr("data-id")}
                success: function(data) {
                    alert(data);
                    $("p").text(data);

                }
            });
   });
</script>

答案 2 :(得分:0)

或者,如果您只想使用一个按钮,则可以使用复选框选择要编辑的行。 试试这个。

<?php
include 'connect-database.php';
$sql  = "SELECT * FROM Kunde WHERE kundenr LIKE '" . $_GET['kundenr'] . "' ";
$stmt = sqlsrv_query($conn, $sql);
if ($stmt == false) {
    die(print_r(sqlsrv_errors(), tue));
}
echo "<table border='1'>";
echo "<tr><th></th><th>Kundenr</th><th>Fornavn</th><th>Etternavn</th><th>Tlf</th><th>Epost</th><th>Produktnr</th><th>Adresse</th></tr>";
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_BOTH)) {
    echo "<tr>";
    echo "<td><input type='checkbox' class='toedit' value='" . $row[0] . "'></td>";
    echo '<td><a href="kundedisplay.php?kundenr=' . $row[0] . '">' . $row[0] . '</a></td>';
    echo "<td>" . $row[1] . "</td>";
    echo "<td>" . $row[2] . "</td>";
    echo "<td>" . $row[3] . "</td>";
    echo "<td>" . $row[4] . "</td>";
    echo "<td>" . $row[5] . "</td>";
    echo "<td>" . $row[6] . "</td>";
    echo "</tr>";
}
echo "<table>";
echo '<button class="redigerkunde" type="button">Rediger Kunde</button>';

在Java Script中,您可以输入:

<script type="text/javascript">
    $("button").on('click', function(){
            $.ajax({
                type: 'POST',
                url: "php/ajax.php",
                data: {id:$('.toedit:checked:first').val()}
                success: function(data) {
                    alert(data);
                    $("p").text(data);

                }
            });
   });
</script>