ajax提交表单mysql_[转载]Jquery+AJAX+PHP+MYSQL 动态web表单增删改查 源代码

HTML部分-----------------------------------------------------------------------------------------

http-equiv="Content-Type" content="text/html;

charset=UTF-8">

Insert title

here

type="text/javascript">

$(function() {

addchange();

$(".bnew").click(bnewclick);

});

//定义修改按钮工具

function addchange() {

$csave = "

class='bsave display1' type='submit' value='保存'

>"

$cbch = "

class='bch' type='button' value='修改'>"

$cbde = "

class='bde'type='button'

value='删除'>"

$(".tdc").html($csave + $cbch +

$cbde);

$(".bch").click(bahclick);

$(".bsave").click(bsaveclick);

$(".bde").click(bdeclick);

}

//开启修改功能

function bahclick() {

//更改修改按钮

$(this).addClass("display1");

$(this).parent().children().eq(2).addClass("display1");

$(this).parent().children().eq(0).removeClass("display1");

//取原值

$thiss =

$(this).parent().parent().children()

$cid = $thiss.eq(0).text();

$cname = $thiss.eq(1).text();

$csex = $thiss.eq(2).text();

if($csex == "男") {

$secn = "selected='selected'";

$secv = ""

} else {

$secn = "";

$secv = "selected='selected'"

}

$csrc = $thiss.eq(3).text();

//定义修改框体

$inputname = "

type='text' name='name' value='" + $cname +

"'>";

$inputsex =

"

value='男' " + $secn +

">男

value='女'" + $secv +

">女

";

$inputscore = "

type='text' name='name'value=" + $csrc + ">";

$thiss.eq(1).html($inputname);

$thiss.eq(2).html($inputsex);

$thiss.eq(3).html($inputscore);

};

//修改保存功能

function bsaveclick() {

//AJAX取值

$thiss =

$(this).parent().parent().children()

$id = $thiss.eq(0).text();

$name =

$thiss.eq(1).children().val();

$sex =

$thiss.eq(2).children().val();

$src =

$thiss.eq(3).children().val();

//AJAX提交

$.post("new1.php", {

full : [$id, $name, $sex, $src,

'1']

}, function(msg) {

});

//更改保存按钮

$(this).addClass("display1");

$(this).parent().children().eq(1).removeClass("display1");

$(this).parent().children().eq(2).removeClass("display1");

//AJAX回传验证

$.post("new1.php", {

full : [$id, $name, $sex, $src,

'2']

}, function(msg) {

obj = $.parseJSON(msg);

//alert(obj[1]+obj[2]+obj[3]);

$thiss.eq(1).html(obj[1]);

$thiss.eq(2).html(obj[2]);

$thiss.eq(3).html(obj[3]);

});

};

//新增功能

function bnewclick() {

//AJAX取值

$thiss =

$(this).parent().parent().children()

$id =

$thiss.eq(0).children().val();

$name =

$thiss.eq(1).children().val();

$sex =

$thiss.eq(2).children().val();

$src =

$thiss.eq(3).children().val();

//未填项目验证

if($id == '' || $name == '' || $src

== '') {

alert("有项目未填写")

} else {

//AJAX提交

$.post("new1.php", {

full : [$id, $name, $sex, $src,

'3']

}, function(msg) {

//验证ID重复

if(msg == 1) {

alert("ID号重复");

$thiss.eq(0).children().val("");

} else {

//AJAX回传验证

$.post("new1.php", {

full : [$id, $name, $sex, $src,

'2']

}, function(msg) {

//增加新项目

obj = $.parseJSON(msg);

$a = "

class='trc'>

" +

obj[0] +

"

"

+ obj[1] +

"

"

+ obj[2] +

"

"

+ obj[3] + "

class='tdc'>

"

$thiss.parent().before($a);

addchange()

$thiss.eq(0).children().val("");

$thiss.eq(1).children().val("");

$thiss.eq(3).children().val("");

})

}

})

}

};

//删除功能

function bdeclick() {

$r = confirm("是否确认删除")

if($r == true) {

$thiss =

$(this).parent().parent();

$id =

$thiss.children().eq(0).text();

$.post("new1.php", {

full : [$id, '', '', '', '4']

}, function(msg) {

//alert($id);

$thiss.remove();

})

}

};

td {

width: 100px;

}

input, option, select {

width: 95px;

}

.bch, .bde {

width: 50px;

}

.bsave, .bnew {

width: 100px;

}

.add {

border: 1px red solid

}

.display1 {

display: none

}

id="test">

id="tab">

学号姓名性别分数

include 'new1.php';

$xd = new new1();

echo

$xd->five();

?>

name='id'>

name='name'>

value='男'>男

value='女'>女

name='src'>

value="新增" class="bnew">

PHP部分-----------------------------------------------------------------------------------------

$arr = $_POST ['full'];

$conn = mysql_connect ( "localhost",

"root", "" );

mysql_select_db ( "test", $conn

);

mysql_query ( "set names 'utf8'"

);

switch ($arr [4]) {

case 1 : //更新

$sql = "update  sus

set name=" . "'" . "$arr[1]" . "'" . ",sex=" . "'" . "$arr[2]" .

"'" . ",src=$arr[3] where id=$arr[0]"; //sql语句

$result = mysql_query ( $sql

);

break;

case 2 : //验证

$sql = "select * from

sus where id=$arr[0]";

$result = mysql_query ( $sql

);

$row = mysql_fetch_array ( $result

);

echo json_encode ( $row );

break;

case 3 : //新增

$sql = "select * from

sus where id=$arr[0]";

$result = mysql_query ( $sql

);

$amount = mysql_num_rows ( $result

);

if ($amount != 0) {

echo $amount;

} else {

$sql = "insert into

sus (id,name,sex,src)value (" . $arr [0] . ",'" .

$arr [1] . "','" . $arr [2] . "'," . $arr [3] . ")"; //sql语句

$result = mysql_query ( $sql

);

echo $sql;

}

;

break;

case 4 : //删除

$sql = "delete

from  sus where

id=$arr[0]";

$result = mysql_query ( $sql

);

break;

}

class new1 {//数据库遍历

public function five() {

$conn = mysql_connect ( "localhost",

"root", "" );

mysql_select_db ( "test", $conn

);

mysql_query ( "set names 'utf8'"

);

$sql = "select * from sus order by

id";

$result = mysql_query ( $sql

);

while ( ! ! $row = mysql_fetch_array

( $result ) ) {

echo "

class='trc'>

$row[0]$row[1]$row[2]$row[3]

class='tdc'>

";

}

mysql_close ( $conn );

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值