【问题标题】:call php function on clicking link in php点击php中的链接调用php函数
【发布时间】:2015-02-12 05:08:47
【问题描述】:

我已经创建了基本的自定义模块。因为我刚刚填写了信息表,该信息将存储到数据库中。以及我以表格格式显示的数据。现在我想通过点击链接编辑和删除记录。

我想在点击以下链接时调用 php 函数

链接是:

while($data = $result->fetchObject()){
$rows[] = array(

  $data->id,
  $data->name,
  $data->address,
  $data->mob,
  $data->gen,
  $data->email,
  $data->hob,
  l('Edit' .$data->id,'/table', array('query' => array('edi'=>$data-          >id))),  
  l('Delete' .$data->id, '/table', array('query' =>   array('del'=>$data->id))),
);

}

功能如下:

function form_values_edit($id){
$id_val = $id;
$my_object = db_select('demo_forms','n')
->fields('n')
->condition('id', $id_val )
->execute()
->fetchAssoc();
return drupal_get_form('demo_form', $my_object);
}

function delete_confirm($form, &$form_state, $id){
$form['delete'] = array(
'#type' => 'value',
'#value' => $id,
);

return confirm_form(
$form,
t('Are you sure you want to delete this?',
'/table',
t('This action cannot be undone'),
t('Delete'),
t('Cancel')
));
}

function delete_confirm_submit($form, &$form_state) {
$record = $form_state['values']['delete'];
if ($record ) {
  $num_deleted = db_delete('demo_forms')
  ->condition('id', $record )
  ->execute();
  drupal_set_message('The record has been deleted!');
}
$form_state['redirect'] = "/table";
}

谢谢

【问题讨论】:

    标签: php drupal-7 drupal-modules


    【解决方案1】:

    您不能通过单击链接来动态调用 PHP 函数,因为 PHP 是 server side language。但是,如果您加载另一个页面,则在加载该页面之前您可以执行 PHP 代码。

    编辑 如果您需要动态地使用 PHP 函数,我通常会在 AJAX 调用中调用该函数(根据某些人的说法,这可能是错误的)。注意:我通常会为此使用 POST。

    $.ajax(
        url: 'url/to/php/function',
        type: 'POST/GET',
        data: {'data' : data},
        success: function(res) {
            // use the result stored in res
        },
        error: function(res) {
            // use res to get the error result
        }
    );
    

    【讨论】:

      【解决方案2】:

      只需在您的 php 代码中添加对 id 字段或其他唯一值的检查,然后更新或删除 id = ... 的行。如果没有唯一值,您将无法这样做。描述您的可用性并发布您的 html 以获取更多信息...

      【讨论】:

        猜你喜欢
        • 2012-01-29
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        相关资源
        最近更新 更多