在php中点击链接调用php函数

时间:2015-02-12 05:08:47

标签: php drupal-7 drupal-modules

我创建了基本的自定义模块。因为我只填写了信息表格,并且该信息将存储在数据库中。我将这些数据显示为表格格式。现在我想编辑和删除点击链接的记录。

我想点击以下链接调用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";
}

由于

2 个答案:

答案 0 :(得分:0)

您无法通过单击链接动态调用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
    }
);

答案 1 :(得分:0)

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