Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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 发送表单值-ajax/php/mysql_Javascript_Php_Ajax_Forms_Form Submit - Fatal编程技术网

Javascript 发送表单值-ajax/php/mysql

Javascript 发送表单值-ajax/php/mysql,javascript,php,ajax,forms,form-submit,Javascript,Php,Ajax,Forms,Form Submit,i wana将表单元素发送到DB(使用php ajax,但不使用jquery) 所以我在w3schools.com上找到了这些教程 教程发送一个元素的值…(不带提交按钮) 但现在我想通过点击提交按钮发送所有元素的值: <form> <input name="name1" type="text"> <input name="name1" type="text"> <input type="submit" value> </from&g

i wana将表单元素发送到DB(使用php ajax,但不使用jquery)

所以我在w3schools.com上找到了这些教程

教程发送一个元素的值…(不带提交按钮) 但现在我想通过点击提交按钮发送所有元素的值:

<form>
 <input name="name1" type="text">
 <input name="name1" type="text">
 <input  type="submit" value>
</from>

如何将name1和name2的值发送到“getuser.php”


tnx

您可以在提交表单时调用javascript函数

<form id="tt" onsubmit="return sendValues('getuser.php', this.id);">
    ...

要在没有Ajax的情况下将输入值发送到另一个页面,可以在表单元素中使用“action”属性

<form method="post" action="getuser.php">
    ...

...
使用此脚本

<script type="text/javascript">
        function loadXMLDoc() {
            var xmlhttp;
            var val=document.getElementById('name1').value;

            var val1=document.getElementById('name2').value;
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("display").innerHTML = xmlhttp.responseText;
                    console.log(xmlhttp.responseText);

                }
            }

            xmlhttp.open("GET", "getuser?value="+val+"&value2="+val1, true);
            xmlhttp.send(null);
        }


    </script>

函数loadXMLDoc(){
var-xmlhttp;
var val=document.getElementById('name1')。值;
var val1=document.getElementById('name2').value;
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“display”).innerHTML=xmlhttp.responseText;
log(xmlhttp.responseText);
}
}
open(“GET”、“getuser”value=“+val+”&value2=“+val1,true);
xmlhttp.send(空);
}
在html中

 <form>
     <input id="name1" type="text">
     <input id="name2" type="text">
    <input type="button" value="submit" onclick='loadXMLDoc()'/>
    </from>

<div id="display">
//to show the response
</div>

//显示响应

这并不能回答您的问题,但w3schools是一个非常糟糕的资源。查看详细信息。删除标记并添加事件处理程序和提交应该是一个按钮您可能不使用jQuery,但是如果没有JavaScriptEnd,您不能使用AJAX(异步JavaScript和XML)。end标记也是错误的-它说这不是您应该问的问题,然后复制粘贴解决方案。这类问题应该通过阅读一些教程(不仅仅是示例)来解决,因为它太基础了,这表明你还没有以结构化的方式学习基础知识。啊,太无聊了。我会更新我的答案。@aksu-更新了我的答案以实际使用Ajax。谢谢Nambi Narayanan。它是有效的。@user2726957如果它真的对你有帮助,请接受我的答案
 <form>
     <input id="name1" type="text">
     <input id="name2" type="text">
    <input type="button" value="submit" onclick='loadXMLDoc()'/>
    </from>

<div id="display">
//to show the response
</div>