简体   繁体   English

如何在php中使用内联jquery

[英]How to use Inline jquery in php

This code works perfectly with a href=""> link, but i need jquery inline function to do this, if i click edit(BUTTON) it should update the table details as well as delete. 此代码与href =“”>链接完美配合,但我需要jquery内联函数来执行此操作,如果我单击编辑(按钮)它应更新表详细信息以及删除。 help me out please :( 请帮帮我:(

1.Index.php 1.Index.php

<html>
<head>
<script>

function validateForm()
{   
var a=document.forms["emp"]["empid"].value;
    var b=document.forms["emp"]["empname"].value;
    var c=document.forms["emp"]["desig"].value;
    var d=document.forms["emp"]["dept"].value;
    //var e=document.forms["emp"]["skill[]"].checked;
    var empskill = document.forms["emp"]["skill[]"]; 
    /*if((emp.skill[0].checked == false) && (emp.skill[1].checked==false) && (emp.skill[2].checked==false))
    {
        alert("Any one skill must be selected");
        return false;
    }*/
    if(a == null || a == "")
    {
        alert("Employee id must be filled out");
        return false;
    }
    if(b == null || b == "")
    {
        alert("Employee name must be filled out");
        return false;
    }
    if(c == null || c == "")
    {
        alert("Employee Designation must be filled out");
        return false;
    }
    if(d == null || d == "")
    {
        alert("Employee department must be filled out");
        return false;
    }
    if((emp.gender[0].checked==false) && (emp.gender[1].checked==false))
    {
        alert("Please select Any one gender"); return false;
    }
    if( (empskill[0].checked==false) && (empskill[1].checked==false) && (empskill[2].checked==false) )
    {
        alert("Any one skill must be selected"); 
        return false; 
    }

}
function numeric(num)
{   var g=/^[0-9]+$/;
    var h=document.getElementById(num).value;
    if(h.match(g))
    {
        return true;
    }
    else
    {
        alert("Enter numeric values only");
        document.getElementById(num).value=' ';
        return false;
    }
    var az = false;
    for ( var i=0; i<document.forms["emp"]["skill[]"].length; i++ ) {
        var ax = document.forms["emp"]["skill[]"][i].checked;
        az = az || ax;
    }
    console.log(az);    

}
function alphabets(t)
{
    var regex = /^[a-zA-Z ]*$/;
    var y=document.getElementById(t).value;
    if(y.match(regex)){
        return true;
    }
    else
    {
        alert("Only Alphabetic characters");
        document.getElementById(t).value=' ';
        return false;
    }


}
</script>
</head>
<body bgcolor="#74AFAD" >
<center>
<br><br><br><h2>
Employee Master<br><br>
<form method="post" action ="addview.php" name="emp" >
<table border=2 cellpadding=10>
<tr><td>Employee code</td> <td><input type="text" name="empid" id="e_id" oninput="numeric(id)"  ></td></tr>

<tr><td>Employee name</td> <td><input type="text" name="empname" id="e-name" oninput="alphabets(id)"></td></tr>

<tr><td>Designation</td> <td><input type="text" name="desig" id="e_desig" oninput="alphabets(id)"></td></tr>

<tr><td>Department</td> <td><select name="dept">

<option value="">Select department</option>
<option value="sales">Sales</option>
<option value="purchase">Purchase</option>
<option value="production">Production</option></td></tr>

<tr><td>Gender</td> 

<td><input type="radio" name="gender" value="male" >Male
<input type="radio" name="gender" value="female" >Female</td></tr>

<tr><td>Skill </td> <td>
<input type="checkbox" name="skill[]" value="sk1"> Sk1
<input type="checkbox" name="skill[]" value="sk2"> Sk2
<input type="checkbox" name="skill[]" value="sk3"> Sk3</td> </tr>

</table>
<br><br>
<input type="submit" name="add" value="Add" onclick="return validateForm();" >
<input type="submit" name="view" value="View" >

</form> 
</center>

</body>
</html>

3.employee.sql(Database) 3.employee.sql(数据库)

-- phpMyAdmin SQL Dump
-- version 4.1.12
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Dec 10, 2015 at 09:44 PM
-- Server version: 5.6.16
-- PHP Version: 5.5.11

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `employee`
--

-- --------------------------------------------------------

--
-- Table structure for table `emp_details`
--

CREATE TABLE IF NOT EXISTS `emp_details` (
  `empid` int(5) NOT NULL,
  `empname` varchar(20) NOT NULL,
  `desig` varchar(20) NOT NULL,
  `dept` varchar(20) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `skill` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `emp_details`
--

INSERT INTO `emp_details` (`empid`, `empname`, `desig`, `dept`, `gender`, `skill`) VALUES
(1, 'Gopi', 'Mg', 'sales', 'male', 'sk1'),
(2, 'Nathan', 'Ceo', 'purchase', 'female', 'sk1'),
(3, 'gows', 'busin', 'sales', 'female', 'sk1');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2.addview.php 2.addview.php

<script>
function checkDelete()
{
    return confirm('Are you sure?');
}
</script></head>
<body bgcolor="#74AFAD" >
<?php

$empid=$_POST['empid'];
$empname=$_POST['empname'];
$desig=$_POST['desig'];
$dept=$_POST['dept'];
@$gender=$_POST['gender'];
@$skill=$_POST['skill'];

//$_SESSION['user']=$_POST['empname'];

//echo "Welcome   ".$_SESSION['user']; 

$con=mysql_connect("localhost","root","") or die("Error connecting to MYSQl");

mysql_select_db("employee",$con);

if(isset($_POST['add']))
{
if(isset($_POST['skill']))
{
    $skill=implode(",",$_POST['skill']);
}

$query="insert into emp_details values ('$empid','$empname','$desig','$dept','$gender','$skill')" ;
mysql_query($query) or die(mysql_error());
header("location:view.php");
}
if(isset($_POST['view']))
{
$query="select empid,empname,desig,dept,gender,skill from emp_details ORDER BY empid DESC";
$results=mysql_query($query) or die(mysql_error());
echo "<center><br><br><br><table border=2>";
echo "<th>Employeeid</th><th>Employeename</th><th>Designation</th><th>Department</th><th>Gender</th><th>Skill</th><th>Action</th>";
while($row=mysql_fetch_array($results,MYSQL_ASSOC)){
echo '<tr>';
echo "<td>".$row['empid']."</td>";
echo "<td>".$row['empname']."</td>";
echo "<td>".$row['desig']. "</td>";
echo "<td>".$row['dept']. "</td>";
echo "<td>".$row['gender']. "</td>";
echo "<td>".$row['skill']. "</td>";
?>
<td><a href='update.php?empid="<?php echo $row['empid'];?>"' value="Edit">Edit</td> 
<td><a href='del.php?empid="<?php echo $row['empid'];?>"' onclick="return checkDelete()">Delete</td></tr>
<?php
}

}
if(isset($_POST['edit']))
{
    header("location:update.php");
}
?>

3.update.php 3.update.php

<?php
error_reporting(1);
include("connect.php");

if(isset($_POST['submit']))
{
    $empid=$_POST['empid'];
        echo $empid;

    $empname=$_POST['empname'];
    $desig=$_POST['desig'];
    $dept=$_POST['dept'];
    $gender=$_POST['gender'];
    $skill=$_POST['skill'];
    if(isset($_POST['skill']))
{
    $skill=implode(",",$_POST['skill']);
}

    $query="update emp_details set empname='$empname', desig='$desig', dept='$dept', gender='$gender', skill='$skill' where empid='$empid'";
    mysql_query($query);
    header("location:index.php");
}

?>
  1. Delete.php Delete.php

Use jquery 'onclick' function to trigger submission on clicking the button. 使用jquery'onclick'函数触发单击按钮时的提交。 Then use the jquery ajaxform to submit the form asynchronously to the server side php script You'll have to use the jqueryform.js to use ajaxform jquery. 然后使用jquery ajaxform将表单异步提交到服务器端php脚本您必须使用jqueryform.js来使用ajaxform jquery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM