首页 > 编程知识 正文

php提交html的表单(php提交表单的方式)

时间:2023-12-20 16:55:06 阅读:318530 作者:AMGL

本文目录一览:

怎么用php把html表单内容写入数据库

1:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)

2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。

具体示例:

(1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。

?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 

VALUES ('Peter', 'Griffin', '35')");

mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 

VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con);

?

(2)其次创建一个 HTML 表单,这个表单可把新记录插入 "Persons" 表。

html

body

form action="insert.php" method="post"

Firstname: input type="text" name="firstname" /

Lastname: input type="text" name="lastname" /

Age: input type="text" name="age" /

input type="submit" /

/form

/body

/html

(3)接着当用户点击上例中 HTML 表单中的提交按钮时,表单数据被发送到 "insert.php"。"insert.php" 文件连接数据库,并通过

$_POST 变量从表单取回值。然后,mysql_query() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。

?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)

VALUES

('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";

mysql_close($con)

?

HTML代码 和PHP代码在一个页面。怎么把html里的表单数据提交给php

test.php页面

判断该页是否提交,如果提交了,就按照正常的接受数据来就行了。

?php

if($_POST){

    $words = $_POST["words"];

    if ($words) {

        echo "收到";

    }

}

 ?

 !DOCTYPE html

 html lang="en"

 head

     meta charset="UTF-8"

     titleDocument/title

 /head

 body

   form action="test.php" method="post"

       input type="text" name="words"

       input type="submit" name="" value="提交"

   /form

 /body

 /html

php提交html标签

你最终的目的其实是不想让这个文本框中的内容被编辑,而且好做样式是吧?

如果是这样,你可以使用JS来实现,但是提交的根本还是利用form表单

其中只是将提交用的表单给隐藏了,在提交之前,将div中的内容放到hidden表单的value中去。

特别注意,因为text以及hidden表单不支持多行文本,所以在id为myData的div中,不可以出现换行字符!

也就是说,div id="myData"?php  这一行中的    和  ?php 之间,不可以出现空格,以及换行,要紧紧挨着,同样后面的  ?和/div之间也是! 切记!

!DOCTYPE html

html

head

title二级列表/title

style type="text/css"

#myData{

width:200px;

margin: 0 auto;

background-color: #EEE;

height:300px;

}

/style

script type="text/javascript"

function t(){

document.getElementById("t5").value = document.getElementById("myData").innerText;

}

/script

/head

body

div id="myData"?php

date_default_timezone_set('Etc/GMT-8');//*修改默认北京地区

$s = rand(111111,999999);// $s 为返回1到15之间的随机数

//echo date(‘y-m-d h:i:s’,time());

echo date("$s"."-"."Ymdhis");

//echo "$s";//输出 即可

?/div

form action="b.php" method="post" onsubmit="t()"

input type="hidden" name="title5" value="" id="t5" /

input type="submit" value = "提交" /

/form

/body

/html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。