不能使用jquery运行服务器端脚本

Can't run server side script using jquery

本文关键字:服务器端 脚本 运行 jquery 不能      更新时间:2023-09-26

首先,提前感谢您在这方面给我的任何帮助!

对,我想做的是,调用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";
  ?>