2011-11-01 19 views
8

पढ़ने के लिए PHP प्राप्त करने का प्रयास कर रहा हूं, मैं अपने दिमाग को रैक कर रहा हूं और पूरे दिन Google को खोज रहा हूं कि यह क्यों काम नहीं करेगा। अपनी साइट को बेहतर बनाने के लिए एक प्रयोग के रूप में मैं http://tutorialzine.com/2009/09/simple-ajax-website-jquery/ सबसे महत्वपूर्ण रूप से AJAX/jquery अनुभाग में एक ट्यूटोरियल देख रहा था।AJAX पोस्ट

असल में, यह ट्यूटोरियल पोस्ट पृष्ठ संख्याओं के साथ काम करता है, हालांकि मैं इसके बजाय पेज नामों का उपयोग करने के लिए इसे बदलने की कोशिश कर रहा हूं। तो यह एक href और php से बाद में Google होम (हैशबैंग्स को Google संगतता लागू करने के लिए) ले जाएगा, इसे "home.html" के रूप में पार्स कर सकता है और उसे सामग्री div में लोड कर सकता है। मेरे बाहर कारणों के लिए, यह सिर्फ काम नहीं करेगा।

var datastring=url.replace('#!',''); //strip the #page part of the hash and leave only the page number 

$('#loading').css('visibility','visible'); //show the rotating gif animation 

$.ajax({ //create an ajax request to load_page.php 
    type: "POST", 
    url: "load_file.php", 
    data: datastring, //with the page number as a parameter 
    dataType: "html", //expect html to be returned 
    async: false, 
    success: function(msg){ 

     if(parseInt(msg)!=0) //if no errors 
     { 
      $('#content').html(msg); //load the returned html into pageContet 
      $('#loading').css('visibility','hidden'); //and hide the rotating gif 
     } 
    } 

}); 

और पूरे php फ़ाइल:

जावास्क्रिप्ट लोडर (केवल अंत खंड जो मैं संशोधित) से: मैं कोड के प्रासंगिक अनुभाग मैं अपने पक्ष में संशोधित करने के लिए प्रयास किया है पोस्ट करेंगे

<?php 
$url = $_POST['datastring']; 

if(file_exists(''.$url.'.html')) 
echo file_get_contents(''.$url.'.html'); 

else echo 'There is no such page!'; 

?> 

मैं अपने आप को सीखना चाहता था और इसे समझना चाहता था लेकिन ईमानदारी से मुझे यह नहीं मिला:/जहां तक ​​मैं कह सकता हूं वहां कोई क्रॉस डोमेन समस्या नहीं है। किसी को पता है कि मैं क्या खो रहा हूँ? अनुमान लगाया गया है कि मैं यहां से पूछूंगा क्योंकि यह उस ट्यूटोरियल की साइट से अधिक यात्रा करने की संभावना है, हालांकि अगर मुझे कोई समाधान मिल जाए तो मैं वहां जाउंगा और टिप्पणियों में भी पोस्ट करूंगा ताकि अन्य मेरे दर्द से बच सकें। एक्सडी

+0

क्या है त्रुटि संदेश मिल रहा है? –

+1

आपको अपने आप को सीखने में मदद करने की भावना में, एक संकेत: यह अनुरोध है कि आप क्या सोचते हैं, और क्या आप सोच रहे हैं कि आप php में क्या सोच रहे हैं? .ajax दस्तावेज़ देखें, फ़ायरबग/आदि का उपयोग करें। अनुरोध की जांच करने के लिए, और php अनुरोध पैरामीटर/मानों को डीबग करें :) –

+0

मान लीजिए कि वे आपको बताएंगे :) –

उत्तर

6

आप एक प्रमुख, मूल्य जोड़ी के रूप में अपने पद डेटा पास करने की जरूरत है।

इस के लिए अपने डेटा को बदलने का प्रयास करें:

data: "datastring="+datastring 
+0

अहह्ह यह है, मुझे नहीं पता कि मैं इसे उम्र के लिए समझ सकता था .. हे। धन्यवाद! –

0

आपका $_POSTdatastring कुंजी के लिए पूछ रहा है, लेकिन मैं इसे अपने AJAX के साथ भेज नहीं देख सकता।

अपने jQuery AJAX में बदलने का प्रयास करें:

data: datastring 

करने के लिए:

data: 'datastring=' + datastring 
1

आप datastring पैरामीटर कि PHP फ़ाइल की तलाश में है की स्थापना नहीं कर रहे हैं।

js फ़ाइल:

var datastring=url.replace('#!',''); //strip the #page part of the hash and leave only the page number 

$('#loading').css('visibility','visible'); //show the rotating gif animation 

$.ajax({ //create an ajax request to load_page.php 
    type: "POST", 
    url: "load_file.php", 
    data: 'datastring='+datastring, //with the page number as a parameter 
    dataType: "html", //expect html to be returned 
    async: false, 
    success: function(msg){ 

     if(parseInt(msg)!=0) //if no errors 
     { 
      $('#content').html(msg); //load the returned html into pageContet 
      $('#loading').css('visibility','hidden'); //and hide the rotating gif 
     } 
    } 

}); 

अपने PHP फ़ाइल:

<?php 
$url = $_REQUEST['datastring']; 
echo $url; 

if(file_exists(''.$url.'.html')) 
echo file_get_contents(''.$url.'.html'); 

else echo 'There is no such page!'; 

?> 
संबंधित मुद्दे