2013-01-10 77 views
0

首先,非常感谢您为我提供的任何帮助!无法使用jquery运行服务器端脚本

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

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

请帮忙,它杀了我!非常感谢!

+0

是PHP脚本error'ing?如果直接从浏览器访问它,会发生什么情况 – jye265

+0

也尝试在函数中添加preventDefault(),否则表单仍然正在提交并且页面正在重新加载 – jye265

+0

感谢jye265,当我从浏览器访问地址时,它只会加载为纯文本 :? – user1963870

回答

0

尝试阿贾克斯

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

,如果你想还需要检查:

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

使用这个脚本,并尝试

的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"; 
    ?> 
+0

谢谢冠军,我已经用这个替换了我的,并且令人讨厌的是它说“加载完成”。 – user1963870

+0

这意味着它一定是错误的我的PHP文件? – user1963870

+0

是的。所以现在你可以改变你的php文件到我提供的内容去除其他所有东西。 – Champ

相关问题