使用PHP的HTML表单 [英] HTML form with PHP

查看:107
本文介绍了使用PHP的HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个执行一些php代码的表单,而不必打开一个全新的php页面。现在,我熟悉POST,这样我就可以执行一个php文件并使用$ _POST [variable]从HTML表单中调用变量,但是,需要一些时间来打开一个新页面,并且我想要一个这样做的行为正确的时间和地点。



例如,有人可以编写HTML代码,创建一个文本框和一个按钮,当用户按下去,它显示用户在按钮旁边输入的文本。



谢谢!

解决方案

div>

这是一个HTML和PHP代码片段,让你开始。它使用jQuery,并使用AJAX将提交按钮下方的textarea值写入。 p>

 <!DOCTYPE html> 
< html>< head>< title> SO例子< / title>
< script
type =text / javascript
src =http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js> ;
< / script>
< / head>
< body>

< form id =frmname =frm>
< textarea id =txtname =txtrows =4cols =40>
< / textarea>< br />
< input type =submit>< br />
< span id =result>< / span>
< / form>

< script type =text / javascript>
$('#frm')。submit(function(e){
e.preventDefault();
$ .ajax({
url:/ so.php ,输入:post,dataType:html,
data:$('#frm')。serialize(),
success:function(obj){
$('结果').text(obj);
}
});
});
< / script>

< / body>
< / html>

PHP代码片段 [file = so.php]

 <?php 
echo $ _POST ['txt'];


I'd like to have a form that executes some php code without having to open a completely new php page. Right now, I'm familiar with "POST" so that I can execute a php file and call the variables from the HTML form using $_POST[variable] but, it takes time to open a new page, and I want to have a form that does the action right then and there.

For example, can someone write html code that creates a text box and a button, and when the user presses go, it displays the text that the user entered right next to the button.

Thanks!

解决方案

Here's an HTML and PHP snippet to get you started. It uses jQuery and just writes the value of textarea beneath the submit button using AJAX.

HTML Snippet [file=so.html]

<!DOCTYPE html>
<html><head><title>SO Example</title>
<script 
  type="text/javascript" 
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js">
</script>
</head>
<body>

<form id="frm" name="frm">
  <textarea id="txt" name="txt" rows="4" cols="40">
  </textarea><br />
  <input type="submit"><br />
  <span id="result"></span>
</form>

<script type="text/javascript">
$('#frm').submit(function(e){
  e.preventDefault();
  $.ajax({
    url:"/so.php",type:"post",dataType:"html",
    data:$('#frm').serialize(),
    success:function(obj){
      $('#result').text(obj);
    }
  });
});
</script>

</body>
</html>

PHP Snippet [file=so.php]

<?php
echo $_POST['txt'];

这篇关于使用PHP的HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆