簡體   English   中英

無法使用jquery運行服務器端腳本

[英]Can't run server side script using jquery

首先,非常感謝您可能為此提供的任何幫助!

正確,我要執行的操作是調用php腳本以運行服務器端,該腳本接受一個值(將是電子郵件)並將其寫入文本文件。

這是我要運行的.php文件。 我還沒有添加任何電子郵件功能,但是經過數小時的嘗試,我什至無法獲得創建文本文件和目錄的功能。 如果我在IDE中運行它,它將完美運行,它將創建腳本並在底部顯示“ test me”文本。 但是,當它從jquery調用運行時,它所做的所有事情都會讀取文件底部的文本。 這是我在jquery中稱呼它的地方:

$(document).ready(function()
    {
        $("#emailForm").submit(function()
        {
            alert("Beginning jquery");
            $("#div1").load("handleEmailAddresses.php");
            alert("Load called");
            //$.post("handleEmailAddresses.php");
        }); 

我還嘗試了可以​​看到注釋掉的post函數。 什么都沒有。 這是被稱為的php文件:

<html> 

<?php

 $dir = 'myDir';
 if ( !file_exists($dir) ) 
 {
    mkdir ($dir, 0777);//This just gives me r/w access to the folder
 }

 file_put_contents ($dir.'/test.txt', 'Hello File');

 ?>
Test Text
</html>

請幫助,它殺死了我! 非常感謝!

試試Ajax

$.ajax({
      url : '/handleEmailAddresses.php',
  });

並且如果您還要檢查:

     $.ajax({
        url : '/handleEmailAddresses.php',
     }).done(function() {
        console.log('completed');
     });

使用此腳本並嘗試

jQuery:

$(document).ready(function()
    {
        $("#emailForm").submit(function(e)
        {
            e.preventDefault();
            alert("Beginning jquery");
           $("#div1").load("handleEmailAddresses.php", function(response, status, xhr)                    {
                 if (status == "error") 
                 {
                  var msg = "Sorry but there was an error: ";
                  alert(msg + xhr.status + " " + xhr.statusText);
                 }
                 else
                 {
                    alert("Load Compleated");
                 }
           });
        }); 

PHP的:

 <?php

     $dir = 'myDir';
     if ( !file_exists($dir) ) 
     {
       if (!mkdir($dir, 0)) {
            echo('Failed to create folders...');
            exit;
       }
     }
 $r = file_put_contents ($dir.'/test.txt', 'Hello File');

 if ($r)
     echo "Success";
 else
    echo "Error";
  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM