2017-05-18 4 views
0

Ich erstelle eine Benachrichtigungsprüfung über AJAX und PHP.PHP-Variable aus AJAX senden

Ich habe eine Warnung erstellt, die "yep" oder "nein" hängt, wenn die Antwort empfangen wird, jedoch keine Warnung angezeigt wird.

Sende ich meine PHP-Variable korrekt?

PHP:

<?php 
 
session_start(); 
 
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') { 
 
    //Request identified as ajax request 
 

 

 
    //HTTP_REFERER verification 
 
    if($_SESSION['email'] == $_GET['email']) { 
 

 
\t \t require_once 'connect.php'; 
 

 

 
\t \t \t /* FORM VARIABLES */ 
 
\t \t \t $email \t \t \t = strtolower($_GET['email']); 
 

 
\t \t \t try 
 
\t \t \t { 
 
\t \t \t \t $stmt = $pdo->prepare("SELECT * FROM notifications WHERE to=:email"); 
 
\t \t \t \t $stmt->execute(array(":email"=>$email)); 
 
\t \t \t \t $count = $stmt->rowCount(); 
 

 
\t \t \t \t echo $count; 
 

 
\t \t \t } 
 
\t \t \t catch(PDOException $e){ 
 
\t \t \t \t echo $e->getMessage(); 
 
\t \t \t } 
 
\t \t 
 
\t \t } 
 
    else { 
 
     header('Location: http://website.com'); 
 
    } 
 
    } 
 
else { 
 
    header('Location: http://website.com'); 
 
} 
 
?>

AJAX:

\t \t \t <script> 
 
\t \t \t \t function notificationCheck() { 
 
\t \t \t \t \t 
 
\t \t \t \t \t $.ajax({ 
 
\t \t \t \t \t \t url: 'website.com/page.php?email='<? echo $email ?>,   
 
\t \t \t \t \t \t data: "", 
 
\t \t \t \t \t \t success: function (data) { 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t var bubification = data; 
 
\t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t if (bubification === 0) { 
 
\t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t window.alert("Yes"); 
 
\t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t } else { 
 
\t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t \t window.alert("Nope"); 
 
\t \t \t \t \t \t \t \t 
 
\t \t \t \t \t \t \t } 
 
\t \t \t \t \t \t \t $('#profile-sidebar-responsive-notifcation-count').html(''+bubification+''); \t \t \t \t \t \t $('#profile-sidebar-notifcation-count').html(''+bubification+''); 
 
\t \t \t \t \t \t } 
 
\t \t \t \t \t }); 
 
\t \t \t \t } 
 

 
\t \t \t \t $(document).ready(notificationCheck); 
 
\t \t \t \t setInterval(notificationCheck, 10000); 
 
\t \t \t </script>

+1

Ich schätze Ihre URL ist falsch. Sie müssen es absolut machen (zB 'http: //website.com/page.php? ...' oder relativ (zB '/page.php? ...') –

+1

ändern Sie Zu Wenn Sie erhalten 404 Fehler –

+0

Sie haben Antworten unter –

Antwort

0

Versuchen Sie diese. Ich sehe einen Syntaxfehler in der URL. Um einen String in PHP zu verketten, verwenden Sie . und in jQuery verwenden Sie +. Sie haben die Zeichenfolge mit PHP-Blöcke in url nicht verketteten

$.ajax({ 
    url: 'website.com/page.php?email='+'<?php echo $email ?>',   
    method: "GET", 
    success: function (data) { 

    var bubification = data; 

    if (bubification === 0) { 

    window.alert("Yes"); 

    } else { 

    window.alert("Nope"); 

    } 
    $('#profile-sidebar-responsive-notifcation-count').html(''+bubification+'');      $('#profile-sidebar-notifcation-count').html(''+bubification+''); 
    } 
}); 
+1

Bitte erklären Sie, warum, ein Code-Dump ist nicht viel Hilfe für jedermann – Steve

+0

Warum sollte Op versuchen Sie Ihren Code? –

+0

Die Op hat einen Syntaxfehler in der URL, aber dieser Code erstellt einen anderen. Die richtige Syntax wäre ' url: 'website.com/page.php?email=', 'es ist keine Verkettung erforderlich hier, Sie schreiben Javascript mit PHP – Steve

0

In Ihrer Ajax-URL Sie vermissen php

+1

Was bedeuten Sie fehlende php? –

+0

In der Ajax-URL, die er verwendet subodhkalika

+0

dann hinzufügen das in Ihrer Antwort –

0

Ich glaube, Sie Verkettung Fehler haben Änderung

<? echo $email ?> to +'<?php echo $email ?>' 
0

In PHP, wenn Sie AJAX Umgang Sie sind wohl die Antwort immer

echo

bitte entfernen header('Location: http://website.com'); und verwenden Sie so etwas wie

echo 'false' 

Wenn Ihr $email nicht übereinstimmt, senden Sie diesen Anruf an eine andere Seite.