Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t使用jquery运行服务器端脚本_Javascript_Ajax_Jquery - Fatal编程技术网

Javascript Can';t使用jquery运行服务器端脚本

Javascript Can';t使用jquery运行服务器端脚本,javascript,ajax,jquery,Javascript,Ajax,Jquery,首先,非常感谢您在这方面给予我的任何帮助 好的,我想做的是调用一个php脚本来运行服务器端,它接收一个值(将是一封电子邮件)并将其写入一个文本文件 这是我想要运行的.php文件。我还没有添加任何电子邮件功能,但经过几个小时的尝试,我甚至无法让它创建文本文件和目录。如果我在IDE中运行它,它会运行得很好,它会创建脚本并在底部显示“testme”文本。但是,当它从jquery调用运行时,它只读取文件底部的文本。这就是我在jquery中所说的: $(document).ready(function()

首先,非常感谢您在这方面给予我的任何帮助

好的,我想做的是调用一个php脚本来运行服务器端,它接收一个值(将是一封电子邮件)并将其写入一个文本文件

这是我想要运行的.php文件。我还没有添加任何电子邮件功能,但经过几个小时的尝试,我甚至无法让它创建文本文件和目录。如果我在IDE中运行它,它会运行得很好,它会创建脚本并在底部显示“testme”文本。但是,当它从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

     $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";
  ?>
PHP:

$(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

     $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";
  ?>


PHP脚本是否出错?如果直接从浏览器访问,会发生什么情况?如果尝试向函数添加preventDefault(),否则表单仍在提交,页面正在重新加载Hanks jye265,当我从浏览器访问地址时,它只是以纯文本形式加载:?如果我将php文件保存为html并加载,它只会显示文本-“test me”显示的纯文本是什么?尝试将文件\u put\u contents包装到var\u dump()中,以检查它是否无法访问该文件感谢Champ,我用它替换了我的文件,令人恼火的是它显示“加载完成”。这意味着我的php文件一定出了问题?是的。现在您可以将php文件更改为我提供的内容,并删除所有其他内容。