2016-04-10 31 views
1

Aşağıda, kodun tıklatılan düğmenin kimliğini php sayfasına göndermek için ajax kullanmış olduğum bir html formu var, ama php sayfasına herhangi bir değer alamıyorum, boş gönderiyor. Bu konuda yeni başlayan biriyim ve öğreticilerle çalıştım, hata yaptığımı kimse işaret edebilir mi? aşağıdaki gibiajax tarafından gönderilen php sayfasındaki verilere erişemiyor musunuz?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>ABC Fashion</title> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> 
<link rel="stylesheet" type="text/css" href="style.css" /> 
<script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">    </script> 
<script> 
$(document).ready(function(){ 
$("button").click(function(){ 
alert((this).id); 
var id=(this).id; 
$.post("code.php", id); 
}); 
}); 
</script> 
</head> 
<body> 
<div id="main_container"> 
<div class="center_content" align="center"> 
    <div class="center_title_bar" align="center">Latest Products</div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T1.jpg" name="tshirt" alt="" border="0" id="T1"/></a></div> 
     <div class="product_img"><button id="d1" onclick="">Dislike</button></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T2.jpg" name="tshirt" alt="" border="0" id="T2"/></a></div> 
     <div class="product_img"><div class="product_img"><button id="d2" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T3.jpg" name="tshirt" alt="" border="0" id="T3"/></a></div> 
     <div class="product_img"><div class="product_img"><button id="d3" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T4.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d4" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T5.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d5" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T6.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d6" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T7.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d7" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T8.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d8" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T9.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d9" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Levi's Men's Cotton T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T10.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d10" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">UB Stylish T-shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T11.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d11" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
    <div class="prod_box"> 
    <div class="top_prod_box"></div> 
    <div class="center_prod_box"> 
     <div class="product_title"><a href="details.html">Jacob n John T-Shirt</a></div> 
     <div class="product_img"><a href="details.html"><img src="images/T12.jpg" alt="" border="0" /></a></div> 
     <div class="product_img"><div class="product_img"><button id="d12" onclick="">Dislike</button></div></div> 
     <div class="prod_price"><span class="reduce">350$</span> <span class="price">270$</span></div> 
    </div> 
    <div class="bottom_prod_box"></div> 
    <div class="prod_details_tab"> <a href="#" title="header=[Add to cart] body=[&nbsp;] fade=[on]"><img src="images/cart.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Specials] body=[&nbsp;] fade=[on]"><img src="images/favs.gif" alt="" border="0" class="left_bt" /></a> <a href="#" title="header=[Gifts] body=[&nbsp;] fade=[on]"><img src="images/favorites.gif" alt="" border="0" class="left_bt" /></a> <a href="details.html" class="prod_details">details</a> </div> 
    </div> 
</div> 
</body> 
</html> 

Benim php kodudur:

<?php 
$servername = "localhost"; 
$username = "admin"; 
$password = "pwd"; 
$dbname = "testdb"; 


$conn = mysqli_connect($servername, $username, $password, $dbname); 

if (!$conn) { 
die("Connection failed: " . mysqli_error($conn)); 
} 

$num= (int)$_POST[id]; 
$str = $num; 

$tempid = substr_replace($str,"T",0); 
echo $tempid; 
$sql = "INSERT INTO `test` (`disliked`) VALUES ('$num')"; 
if (mysqli_query($conn, $sql) === TRUE) { 
echo "New record created successfully"; 
} else { 
echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
} 

mysqli_close($conn); 
?> 

cevap

1

mesele, hiçbir tanımlayıcı ile id değerini geçiyoruz $.post görüşmesinde.

$.post("code.php", id); 

Buna: Bu

Değişim

$.post("code.php", {id: id}); 
+0

teşekkürler ama çalışmıyor, php tarafında bu değere nasıl erişebileceğinizi söyler misiniz? $ num = (int) $ _ POST [id]; Bu doğru mu yoksa değişiklik yapmak zorunda mıyım? – navs89

1

$ .post ("code.php", id) değiştirin;

var request = $.ajax({ 
           url: "code.php", 
           type: "POST", 
           data: {id:id}, 
           dataType: "html" 
           }); 



        request.done(function(msg){ 
         alert(msg); 
        }); 

         request.fail(function(jqXHR, textStatus) { 
            alert("Request failed: " + textStatus); 
         }); 
1

ile biz Anahtar => değer çiftini göndermek gerekir unutmayın.

Ajax gönderi yönteminize doğrudan geçiş yapıyorsunuz, bu nedenle anahtar eksik ve anahtar kullanarak erişmeye çalışıyorsunuz.

Var params = {id: id}; // birinci Kimlik anahtarı: ikinci kimlik değeridir.

$ .post ("code.php", params);

+0

bu işe yaramadı ... php kodumda bir sorun var mı? – navs89

+0

Parametreler doğru şekilde iletildiyse, konsolun n konsolunda açılabiliyor ol ... –

+0

evet parametreler doğru şekilde iletiliyor. – navs89

İlgili konular