【问题标题】:how to proceed form only with a javascript without php [closed]如何仅使用没有 php 的 javascript 处理表单 [关闭]
【发布时间】:2014-03-01 03:02:35
【问题描述】:

我有一个这样的简单表格:

 <form method="POST" action="do_something.php">
            <label for="name">Your name: </label> <input type="text" id="name" name="name">
            <input type="submit">
        </form>

然后是执行此操作的 php 文件:

<?php
    echo "your name is ". $_POST['name'];
?>

我的任务是启用默认表单发送并在 javascript 中编写我自己的表单发送方法。但我不是 javascript 的大朋友,我绝对不知道从哪里开始。 :( 谁能给我指导或帮助我?谢谢!

【问题讨论】:

  • 我建议从一些研究开始;如果你想从任何人那里得到任何真正的帮助,你需要证明你已经付出了一些努力来尝试自己解决这个问题。

标签: javascript php html forms web


【解决方案1】:

您正在寻找 Ajax 请求 http://en.wikipedia.org/wiki/Ajax_%28programming%29

客户端:

// This is the client-side script

// Initialize the Ajax request
var xhr = new XMLHttpRequest();
xhr.open('get', 'send-ajax-data.php');

// Track the state changes of the request
xhr.onreadystatechange = function(){
    // Ready state 4 means the request is done
    if(xhr.readyState === 4){
        // 200 is a successful return
        if(xhr.status === 200){
            alert(xhr.responseText); // 'This is the returned text.'
        }else{
            alert('Error: '+xhr.status); // An error occurred during the request
        }
    }
}

// Send the request to send-ajax-data.php
xhr.send(null);

服务器端:

<?php
// This is the server-side script

// Set the content type
header('Content-Type: text/plain');

// Send the data back
echo "This is the returned text.";
?>

【讨论】:

  • -1 OP 没有请求 javascript。
  • @qwertynl 不,OP 说“”“不是 javascript 的大朋友,我绝对不知道从哪里开始”“”
  • 操作错误。为他指明正确的方向可能是最好的方法。
  • 好的,我无法回答,所以这里是我的回答截图:d.pr/i/8fJj
  • @user2202284 好吧,我不确定是什么导致了这个错误,但它很可能与您没有将名称作为 Ajax 请求的变量发送这一事实有关。例如,查看我们现在所在页面的顶部 stackoverflow.com/quest/numb/post/numb?noredirect=1 在这种情况下 noredirect 是变量,1 是它的值。您需要将变量名称和值一起发送给您的 php。
【解决方案2】:

唯一存在的客户端代码是 javascript。

这是你唯一可以用来玩表单提交的东西。

你可以使用任何你想要的服务器端语言,它不必是 PHP。

【讨论】:

  • 不正确;还有coffeescript
  • 翻译为javascript @DiMono。浏览器目前仅读取 javascript(2014 年 2 月)。
  • 是的,对不起,我的错。我的意思是使用 javascript 提交表单并将其发送到我使用 javascript 拥有的 php 文件。没有默认方法。
  • 对于服务器端,我建议使用冷融合。 (是的,这是个玩笑)
  • @FlorianMargaine 是的,但只是该浏览器的开发版本;此外,旧版本的 IE 支持 VBScript。
猜你喜欢
  • 2012-04-27
  • 2012-06-28
  • 2013-02-17
  • 2013-03-09
  • 1970-01-01
  • 2012-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多