2016-05-14 133 views
0

我正在尝试提交表单而不刷新页面,我希望在点击按钮id = fav时出现提醒消息。这是代码,但我不知道我做错了什么。它应该是在按钮点击或表单提交?无刷新提交表单(ajax,php)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script> 
$(document).ready(function(){ 
$("#f1").submit(function(){ 




// AJAX Code To Submit Form. 
$.ajax({ 
type: "POST", 
url: "bookpage.php", 
data: {'fav':'fav'} , 
cache: false, 
success: function(response){ 
if(response.message){ 
alert(response.message); 
} 
} 
}); 
} 
return false; 
}); 
}); 
    </script> 
<form action="#read" method="post" id="f1"> 
    <div class="r1"> 
    <button class="down" name="download" title="Add to favorites">Download</button> 
    <li><a class="full" href="full.php?id=<?php echo $id; ?>">Full page</a> 
    </li> 

    <button class="d-later" name="dlater">Download later</button> 
    <button class="fav-button" type="submit" id="fav"></button> 

    </div> 
</form> 

PHP

if(isset($_POST['fav']) && $_POST['fav'] == 'fav' && fav_exists($u , $ii)== true){ 

     $query = "DELETE FROM favorites WHERE bid='$ii' AND email='$u'"; 
     $result = mysql_query($query); 
     if(! $result) { 

      die('Could not delete data: ' . mysql_error()); 

    } $response['message'] = 'My message'; 
echo json_encode($response); 

    }else if(isset($_POST['fav']) && $_POST['fav'] == 'fav' && fav_exists($u , $ii)== false){ 


      $query = "INSERT INTO favorites (email,book, bid) VALUES('$u','$bname','$ii')"; 
     $result = mysql_query($query); 
     if(! $result) { 

      die('Could not enter data: ' . mysql_error()); 
    } 
    $response['message'] = 'My message'; 
echo json_encode($response); 
} 

    function fav_exists($u , $ii){ 


    $query = "SELECT id FROM favorites WHERE email='$u' AND bid='$ii'"; 
    $result = mysql_query($query); 
    $count = mysql_num_rows($result); 


    if($count >= 1) { 
     return true; 
    } else { 
     return false; 
    } 

} 
+0

添加'按钮类型='submit'' – C2486

+0

数据建议立即进行删除d是数据:{'fav':fav}, – JYoThI

+0

没有,没有工作@ Rishi –

回答

0

我想你逝去的空数据,参见下面的例子是如何通过一些数据;如果你想运行AJAX + PHP你需要传递一些数据

<?php if(isset($_POST['action'] && $_POST['action'] == 'my_action') { 
// do stuff; 
// to send json response use json_encode; 
$response['message'] = 'My message'; 
echo json_encode($response); 
} 


$.ajax({ 
url: "my_php.php", 
type: "POST", 
data: { 'action' : 'my_action' }, 
success: function(response){ 
if(response.message){ 
alert(response.message); 
} 
} 
}); 

此外,我强烈建议使用PHP PDO进行SQL查询 - http://php.net/manual/en/book.pdo.php

UPD:

$('#fav').click(function(){ 
    do_ajax_request('you can pass different type of acitons as param'); 
}); 
function do_ajax_request(action) { 
$.ajax({ 
url: "my_php.php", 
type: "POST", 
data: { 'action' : action }, 
success: function(response){ 
    if(response.message){ 
    alert(response.message); 
    } 
} 
}); 
} 

而在你的php文件你可以switchif/else不同的功能取决于你的action;

<?php if(isset($_POST['action'])) { 
    $action = $_POST['action']; 
    switch($action) { 
    case 'favorites': 
    $msg = 'favorites action called'; 
    breake; 
    case 'not fav': 
    $msg = 'not fav called'; 
    breake; 
    default: 
    $msg = 'Nothing passed'; 
    breake; 
    } 
    $Response['msg'] = $msg; 
    echo json_encode($Response); 
} 
+0

在你的元素检查器的'Network'选项卡上,你看到数据是否被传递?你也可以print_r ($ POSTEDVALUE)''在你的php文件中查看它是否通过,通过'Network'标签 –

+0

你可以试试'dataType:html',只需在php端回显一些字符串 –

0

这就是我用于同样的任务。

HTML

<form action="" method="post"> 
name:<input type="text" name="user" /> <br/> 
<p><input type="submit" /></p> 
</form> 

JS

$(function() { 
    $('form').submit(function(e) { 
     e.preventDefault(); // Stop normal submission 
     data = $('form').serializeArray(); 
     alert(JSON.stringify(data)); 
     $.ajax({ 
      type: 'POST', 
      contentType: "application/json; charset=utf-8", 
      url: 'inc/update.php', 
      data: { 
       json: JSON.stringify(data) 
      }, 
      dataType: 'json' 
      } 
     }); 
    }); 
    return false; 
}); 

PHP

$str_json = file_get_contents('php://input'); 
$str_json = urldecode($str_json); 
$str_json = str_replace('json=[', '', $str_json); 
$str_json = str_replace(']', '', $str_json); 
$arr_json = json_decode($str_json, true); 
$name = $arr_json['name']; 

$pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password); 
$update = $pdo->prepare("UPDATE user SET name='".$name."' WHERE id='3';"); 
$update->execute();