简体   繁体   English

Ajax函数在同一个分页中调用php函数不起作用

[英]Ajax function to call php function in same pag doesn't work

My problem is that Ajax in same php file as php function I want to call doesn't work. 我的问题是与我要调用的php函数相同的php文件中的Ajax无法正常工作。 I want to make that when I press button the page won't reload and the function would be called. 我想确保当我按下按钮时页面不会重新加载,并且该函数将被调用。 I putted my tag with ajax on the bottom and php function at the top of page if that makes sense. 如果可以的话,我在页面的底部放置了ajax标签,在页面顶部放置了php函数。 Here's my code : 这是我的代码:

Ajax : 阿贾克斯:

<script>
          function getData() {
              $.ajax({
                    type: "GET",
                    url: "view.php",
                    data: {action: 'PakeistiCashreg'}, 
                    success: function(data){
                        alert('Kasos aparatas sėkmingai pakeistas');
                    }
                  });
                }
          </script>

Php function I want to call : 我想调用的PHP函数:

if(isset($_GET["action"])) {
   if($_GET["action"] == "PakeistiCashreg") {
      mysql_query("UPDATE ".$table['invoices']." SET `CashReg` = '".$_POST['CashRegInput']."' WHERE `id` = '156' LIMIT 1");
   }
}

And the button where action happens : 以及发生动作的按钮:

<form method="post"><?php echo CASHREG; ?>:<select name="CashRegInput" id="CashRegInput" class="Input3"><option></option>
                    <?php
                    foreach($CashReg as $key => $value) {
                        echo '<option value="'.$value.'">'.$value.'</option>';
                    }
                    ?>
                </select><button name="PakeistiCashreg" onclick="getData();" id="PakeistiCashreg">Pakeisti kasos aparatą</button></form>

try this : Markup: 试试这个:标记:

<form name="some-form" method="post" onsubmit="getData(event);">
    <?php echo CASHREG; ?>:
    <select name="CashRegInput" id="CashRegInput" class="Input3">
        <option></option>
        <?php
            foreach($CashReg as $key => $value) {
                echo '<option value="'.$value.'">'.$value.'</option>';
            }
        ?>
    </select>
    <button type="submit" name="PakeistiCashreg" id="PakeistiCashreg">Pakeisti kasos aparatą</button>
</form>

script: 脚本:

function getData(e) {
    e.preventDefault();
    var cashRegInput = $('#CashRegInput').val();
    if (cashRegInput) {
        $.ajax({
            type: "POST",
            url: "view.php",
            data: { action: 'PakeistiCashreg', CashRegInput: cashRegInput },
            success: function(data) {
                alert('Kasos aparatas sėkmingai pakeistas');
            }
        });
    }
}

Let me know if it works for you. 请让我知道这对你有没有用。

I'm not sure, but I think you're saying that you've got a file view.php which contains both the page which is originally loaded and the handler for the ajax call. 我不确定,但是我想您是说您有一个文件view.php ,其中包含最初加载的页面和ajax调用的处理程序。 If that's not what you're saying then ignore this answer. 如果这不是您要说的,请忽略此答案。

Doing an ajax request is not the same as loading a page. 进行ajax请求与加载页面不同。 When the client receives the ajax response, all it does is call the appropriate handler. 当客户端收到ajax响应时,它所做的只是调用适当的处理程序。 In this case, all it will do is alert('Kasos aparatas sėkmingai pakeistas'); 在这种情况下,它所要做的只是alert('Kasos aparatas sėkmingai pakeistas'); . It's attempting to run the handler when the page is first loaded, except that you've added a check on $_GET("action") which should prevent this. 它试图在首次加载页面时运行处理程序,只是您已在$_GET("action")上添加了一个检查以防止这种情况发生。 You need a separate URL and a separate handler for the ajax call. 对于ajax调用,您需要一个单独的URL和一个单独的处理程序。 For example, you could change the ajax call URL to action.php , then move the snippet with the database call into a separate file called action.php . 例如,您可以将ajax调用URL更改为action.php ,然后将包含数据库调用的代码段移动到名为action.php的单独文件中。

(Actually, PHP is really only intended for returning HTML, not for ajax calls. so it would be better to replace action.php with a handler in some other language/framework. But PHP will work, and in any case I don't know enough about your setup to advise on how to do this.) (实际上,PHP实际上仅用于返回HTML,而不用于ajax调用。因此,用其他某种语言/框架的处理程序替换action.php会更好。但是PHP可以工作,在任何情况下我都不会足够了解您的设置,以提供有关如何执行此操作的建议。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM