2012-12-01 26 views
0

我當前的代碼,我有我的網站是在底部以下,我會很快transfering代碼到我profile.php :)阿賈克斯如何發送一個形式,沒有它重裝

test.php的

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/ 
libs/jquery/1.3.0/jquery.min.js"> 
</script> 
<script type="text/javascript" > 
$(function() { 
$("#ntb").click(function() { 
var nt = $("#nt").val(); 
var data=$('#nt').serialize(); 
if(nt=='') 
{ 
$('.success').fadeOut(200).hide(); 
$('.error').fadeOut(200).show(); 
} 
else 
{ 
$.ajax({ 
type: "POST", 
url: "../ajax/post.php", 
data: dataString, 
success: function(){ 
$('.success').fadeIn(200).show(); 
$('.error').fadeOut(200).hide(); 
} 
}); 
} 
return false; 
}); 
}); 

</script> 


<form method="post" name="form"> 
<input id="nt" name="nt" type="text" /> 
<input type="submit" style="display:block;" name='ntb' id='ntb' class="Buttonchat"  value="Post" /> 
</form> 
<div> 
<span class="error" style="display:none"> Please Enter Valid Data</span> 
<span class="success" style="display:none"> Registration Successfully</span> 
</div></form&gt; 

上面這段代碼是Ajax和HTML,當我在阿賈克斯,但是當我剛纔的HTML和PHP它張貼到數據庫,IDK的爲什麼雖然:(

真的不工作。 ./ajax/post.php

<?php 

include '../core/init.php'; 

if(isset($_POST['nt']) && isset($_POST['ntb'])){ 
mysql_query("INSERT INTO `post` (`myid`, `text`) VALUES ('".$_SESSION['id']."',   '".mysql_real_escape_string($_POST['nt'])."')"); 
} 
?> 

以上是我的PHP發佈的數據,該工程100%:) 謝謝您的幫助

回答

1

我做這種方式:

function submitMe(val) { 
    jQuery(function($) {  
     $.ajax({   
      url : "some_php_page.php?action="+val, 
      type : "GET", 
      success : function(data) { 
       alert ("works!"); //or use data string to show something else 
       } 
      }); 
     }); 
    } 

然後我分析在actionsome_php_page.php?

這樣來做:

//function that get value from the request object and parses it to a variable; or stopps the script if no value is found 
function getanyValue($param) { 
    if (isset($_GET[$param])) { 
     return $_GET[$param]; 
    } else { 
     die(''); 
    } 
} 
//get the action value 
$action = getanyValue('action'); 
//add action to the database 
$inserId = addAction($action); 

public $dbserver = ''; 
public $dbusername = ''; 
public $dbpassword = ''; 
public $dbname = ''; 

function openDb() { 
    try { 
     $db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . ''); 
    } catch (PDOException $e) { 
     die("error, please try again"); 
    } 
    return $db; 
} 

function addAction($action) { 
    $query = "INSERT INTO action_table (action_column) VALUES(?)"; 
    $db = $this->openDb(); 
    $stmt = $db->prepare($query); 
    $stmt->bindValue(1, $action, PDO::PARAM_STR); 
    $stmt->execute(); 
    //get the action_table id from the database from the new insert 
    return $db->lastInsertId('id'); 
} 
+0

是的,但我如何得到它,因此它可以直接通過數據庫,而無需使頁面重新加載? – Bob

+0

我增加了更多的代碼; 'some_php_page.php'。 PS-不要忘記給幫助你的用戶增加聲望,並在你解決問題時接受正確的答案。 – Andrew

1

您不一定需要AJAX才能在不重新加載的情況下提交表單。一個方便的技巧是簡單地提交到隱藏的iframe中:

<iframe style="display:none" name="submissiontarget" id="submissiontarget"></iframe> 

<form method="post" target="submissiontarget" action = "../ajax/post.php" name="form"> 
<input id="nt" name="nt" type="text" /> 
<input type="submit" style="display:block;" name='ntb' id='ntb' class="Buttonchat"  value="Post" /> 
</form> 
<div> 
<span class="error" style="display:none"> Please Enter Valid Data</span> 
<span class="success" style="display:none"> Registration Successfully</span> 
</div> 
</form> 
+0

我是否需要向該代碼添加其他任何內容以使其可以正常工作? – Bob

+0

@KaniRobinson Nope。重要的是要注意你的JS不再工作,但是你並不需要它。 –

+0

@KaniRobinson你試過了嗎? –

1

問題是,您正在將'dataString'傳遞給PHP腳本。我可以看到'dataString'沒有在任何地方定義。

所以你會一直做的是:

var dataString = 'nt=' + data + '&ntb=whateverntbshouldbeequalto'; 

這應該做的伎倆!

-1

首先,我會嘗試打印出您的$_POST以查看要發送的內容。看起來你的數據沒有正確的形成。你正在測試你的php中的兩個seprate變量,但似乎你只是發送一個在你的文章data部分ajax文章。