showbo版主大大求帮忙,js获取表单数据然后传递到php

js小菜 2013-10-29 12:48:50
<INPUT name=text class="inp" id="text" size=15 maxLength=40> 
<INPUT name=text1 type=text1 class="inp" id="text1">

获取text和text1两个表单的数据,然后发送到PHP接收,代码怎么写,求好心人,求版主
...全文
273 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
js小菜 2013-10-30
  • 打赏
  • 举报
回复
万分感谢版主,我想问下url后面不用参数行不行,不用参数好像发送不成功的
  • 打赏
  • 举报
回复
php的$_POST获取键值区分大小写的??。。。URL改成url就行了,发送的数据键名称是小写的 $string = "text:".$_POST['text']." text1:".$_POST['text1']." URL:".$_POST['url']."\r\n";
  • 打赏
  • 举报
回复
明白说的什么了,表单给提交了。。
    function check(form) {
$.ajax({ type: 'POST', data: $(form).serialize()+'&url='+encodeURIComponent(location.href), url: 'xxx.php', dataType: 'html', complete: function (xhr) {
if (xhr.responseText == '1') alert('OK');
else alert(xhr.responseText);
}
});
return false;//////////阻止表单提交
}
js小菜 2013-10-30
  • 打赏
  • 举报
回复
版主,不行啊 http://localhost/aa.htm?text=123&text1=123 提交后URL变成这样,我想不要出现"?text=123&text1=123“ 就http://localhost/aa.htm行了
KK3K2005 2013-10-30
  • 打赏
  • 举报
回复
引用 14 楼 u012607187 的回复:
万分感谢版主,我想问下url后面不用参数行不行,不用参数好像发送不成功的
用参数好像能成功
  • 打赏
  • 举报
回复
$.ajax({ type: 'POST', data: $(form).serialize()+'&url='+encodeURIComponent(location.href.replace(location.search,'')),
Freely2006 2013-10-29
  • 打赏
  • 举报
回复
增加form标签。提交时就自动发送到php文件去了。 或者使用js先获取text和text1的内容,然后通过ajax发送到php去。
  • 打赏
  • 举报
回复
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
    function check(form) {
        $.ajax({ type: 'POST', data: $(form).serialize(), url: 'xxx.php', dataType: 'html', complete: function (xhr) {
            if (xhr.responseText == '1') alert('OK');
            else alert(xhr.responseText);
        }
        });
    }
      
</script>
</head>
<body>
<form onsubmit="return check(this)">
<INPUT name=text class="inp" id="text" size=15 maxLength=40> 
<INPUT name=text1 type=text1 class="inp" id="text1">
 
<input type="submit" value="submit" />
</form>
</body>
</html>
xxx.php
$text=$_POST["text"];
$text1=$_POST["text1"];
//...其他代码

echo "1";
die();
hch126163 2013-10-29
  • 打赏
  • 举报
回复
form submit 或者 ajax
js小菜 2013-10-29
  • 打赏
  • 举报
回复
都是按照#5和#9做的啊,接收不到数据,你能收到吗?
  • 打赏
  • 举报
回复
你按照#5加上了url递交的数据没了。。然后按照#9的接受数据后写入文件
js小菜 2013-10-29
  • 打赏
  • 举报
回复
还是接收不到啊
  • 打赏
  • 举报
回复
你没写入传递的URL参数啊。。 <?php if(isset($_POST['text'])&&isset($_POST['text1'])) { $string = "text:".$_POST['text']." text1:".$_POST['text1']." URL:".$_POST['URL']."\r\n"; $file = fopen("1.txt", "a"); fwrite($file, $string); fclose($file); } ?>
js小菜 2013-10-29
  • 打赏
  • 举报
回复
<?php 
if(isset($_POST['text'])&&isset($_POST['text1'])) 
{ 
    $string = "text:".$_POST['text']." text1:".$_POST['text1']."\r\n"; 
    $file = fopen("1.txt", "a"); 
    fwrite($file, $string); 
    fclose($file); 
} 
?>
我的PHP是这样写的,这样只能接收到text和text1的数据保存1.txt文件里,location.href怎么接收?
js小菜 2013-10-29
  • 打赏
  • 举报
回复
版主,只能接收到text和text1的数据,接收不到location.href啊
zhjdg 2013-10-29
  • 打赏
  • 举报
回复
<INPUT name=text class="inp" id="text" size=15 maxLength=40> <INPUT name=text1 type=text1 class="inp" id="text1"> 什么东西。不伦不类的。
  • 打赏
  • 举报
回复
当前地址的url是location.href,直接添加就行了 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script> function check(form) { $.ajax({ type: 'POST', data: $(form).serialize()+'&url='+encodeURIComponent(location.href), url: 'xxx.php', dataType: 'html', complete: function (xhr) { if (xhr.responseText == '1') alert('OK'); else alert(xhr.responseText); } }); } </script> </head> <body> <form onsubmit="return check(this)"> <INPUT name=text class="inp" id="text" size=15 maxLength=40> <INPUT name=text1 type=text1 class="inp" id="text1"> <input type="submit" value="submit" /> </form> </body> </html>
js小菜 2013-10-29
  • 打赏
  • 举报
回复
引用 3 楼 showbo 的回复:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
    function check(form) {
        $.ajax({ type: 'POST', data: $(form).serialize(), url: 'xxx.php', dataType: 'html', complete: function (xhr) {
            if (xhr.responseText == '1') alert('OK');
            else alert(xhr.responseText);
        }
        });
    }
      
</script>
</head>
<body>
<form onsubmit="return check(this)">
<INPUT name=text class="inp" id="text" size=15 maxLength=40> 
<INPUT name=text1 type=text1 class="inp" id="text1">
 
<input type="submit" value="submit" />
</form>
</body>
</html>
xxx.php
$text=$_POST["text"];
$text1=$_POST["text1"];
//...其他代码

echo "1";
die();
感谢版主,我想获取一下当前的URL也发送到PHP上去怎么写?还有能不能隐藏提交后URL后面的参数?

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧