2016-05-04 11 views
1

Ich möchte eine Newsletter-Abonnementfunktion für meine Website einrichten. und ich möchte alle Eingabe speichern in eine TXT-Datei im Host-Ordner. Ich möchte jedoch nicht die Seite wechseln, nachdem die Leute sie eingereicht haben. Zeigen Sie einfach eine Popup-Nachricht, die sagt "Danke für die Anmeldung" wird perfekt sein. Ich weiß, wie man es mit PHP macht, um eine Nachricht in einer separaten Seite anzuzeigen, aber nicht sicher, wie man es in einem Popup-Fenster macht. Hier ist mein HTML- und PHP-Code. Bitte helfen Sie, vielen Dank.Formulareingaben in txt speichern, Popup-Ergebnis, keine Seitenänderung

<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <form> 
     <form action="myprocessingscript.php" method="post"> 
     <input name="field1" type="text" /> 
     <input name="field2" type="text" /> 
     <input type="submit" name="submit" value="Save Data"> 
    </form> 
    <a href='data.txt'>Text file</a> 
</body> 

PHP-Funktion

<?php 
if(isset($_POST['field1']) && isset($_POST['field2'])) { 
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n"; 
    $ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX); 
    if($ret === false) { 
     die('There was an error writing this file'); 
    } 
    else { 
     echo "$ret bytes written to file"; 
    } 
} 
else { 
    die('no post data to process'); 
} 
+1

Verwendung AJAX in HTML-Datei. Für Anfragen –

+2

ich wirklich eine Datenbank über eine flache Datei vorschlagen, werden Sie sehr dankbar sein, wie es wächst. –

+0

Html-Formular soll so funktionieren, um den Speicherort beim Senden zu ändern, aber der beste Teil ist, dass Sie es mit JS verhindern können, aber Sie müssen Ajax verwenden, um die Parameter zu senden. –

Antwort

1

ist Sobald Sie jQuery auf Ihre Seite aufgenommen haben, sollte wie folgt funktionieren:

// process the form 
$('form').submit(function(event) { 

    // get the form data 
    // there are many ways to get this data using jQuery (you can use the class or id also) 
    var formData = { 
     'field1'    : $('input[name=field1]').val(), 
     'field2'    : $('input[name=field2]').val() 
    }; 

    // process the form 
    $.ajax({ 
     type  : 'POST', // define the type of HTTP verb we want to use (POST for our form) 
     url   : 'myprocessingscript.php', // the url where we want to POST 
     data  : formData, // our data object 
     dataType : 'json', // what type of data do we expect back from the server 
        encode   : true 
    }) 
     // using the done promise callback 
     .done(function(data) { 

      // log data to the console so we can see 
      console.log(data); 
      // data is the output from your php, check it and display alert appropriately 

      // here we will handle errors and validation messages 
     }); 

    // stop the form from submitting the normal way and refreshing the page 
    event.preventDefault(); 



}); 

Werfen Sie einen Blick auf source article