如何在JavaScript中调用php方法? - javascript

            
        

    

我已经在这里进行研究了。
许多人说ajax更好。
但是我想先问些问题,因为我从不使用ajax。

如果在windows.confirm using javascipt上按yes,我想删除数据库中的某些行。

我的HTML button

<button onclick="deleteFunction()">Del</button>

我的Javascript function

<script type="text/javascript">
  function deleteColumn(id)
  {
    if (confirm("Are you want to delete?") === true) {
      // Do delete method in PHP
    } else {
      // Cancel delete
    }
  }
</script>

我在delete中的Storage class方法

class Storage
{
  public function delete($condition)
  {
    // Delete from database with condition
  }
}

我必须使用ajax呼叫PHP method吗?

参考方案

这应该给您基本的流程:

<!-- the form sample -->
<form method="POST" action="your_php.php" onsubmit="return confirm('Are you want to delete?');">
    ID: <input type="text" name="id" /><br/>
    <input type="submit" name="submit" />
</form>

在PHP中:

<?php

class Storage
{
    public function delete($id)
    {
        // should be better if this is another class
        $connection = new mysqli('localhost', 'your_db_username', 'db_password', 'database_name');
        $stmt = $connection->prepare('DELETE FROM `table` WHERE id = ?');
        $stmt->bind_param('i', $id);
        $stmt->execute();
    }
}

if(isset($_POST['submit'])) {
    $id = $_POST['id'];
    $storage = new Storage();
    $storage->delete($id);
}

?>

打印二维阵列 - javascript

我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…

粗糙的Unicode->没有CLDR的语言代码? - javascript

我在写字典应用。如果用户键入Unicode字符,我想检查该字符是哪种语言。例如字 - returns ['zh', 'ja', 'ko'] العربية - returns ['ar'] a - returns ['en', 'fr', …

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

使用JS和PHP更改弹出窗口背景图像 - javascript

我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …

提交初始化后删除某些帖子数据 - javascript

在初始化提交之后但在将数据发送到处理页面之前,是否可以过滤$ _POST表单数据?我想象过程的方式:提交->收集$ _POST数据->发送数据我想做的事:提交->收集$ _POST数据->删除某些元素->发送数据这样就不必更改处理页面以过滤掉不希望接收的元素了吗? javascript大神给出的解决方案 当然可以,您可以在JS …