我需要从数据库中检索数据,并在php if语句中使用它

时间:2019-07-06 18:13:39

标签: php html mysql ajax

我有一个课程的系统注册,但我遇到这个问题,我希望管理员能够向学生隐藏要注册的表格(就像打开或关闭该系统一样),以及他何时想要使其再次可用

我试图解决的方法是在管理页面中有一个单选按钮,该按钮在数据库中(带有ajax)传递值0或1我想要的是以某种方式检索此值,将其存储到php变量并使用在if语句中,例如

html + ajax

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>OnClick Insert Radio Button value into Database using PDO in 
Jquery Ajax PHP | SoftAOX Tutorial</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script>
</head>
<body>
<h1> On Click Insert Radio Button Value into Database</h1>
<input type="radio" name="status" value= "1" >on<br/><br/>
<input type="radio" name="status" value= "0">off<br/><br/>

<h3 id="result"></h3>
<br/>
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var status = $(this).val();
$.ajax({
url:"insert.php"
method:"POST",
data:{status:status},
success: function(data){
$('#result').html(data);
}
});
});
});
</script>
  </body>
</html>

insert.php

<?php

//Insert Data
$hostname = "localhost";
$username = "root";
$password = "";
$databasename = "onlinecourse";
try {
    $conn = new PDO("mysql:host=$hostname;dbname=$databasename", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if (isset($_POST["status"])) {
        $query = "UPDATE CRS SET status =  (:status)";
        $statement = $conn->prepare($query);
        $statement->execute(
            array(
                'status' => $_POST["status"]
            )
        );
        $count = $statement->rowCount();
        if ($count > 0) {
            echo "Data Inserted Successfully..!";
        } else {
            echo "Data Insertion Failed";
        }
    }
} catch (PDOException $error) {
    echo $error->getMessage();
}

我想要if语句重定向的文件

<?php

session_start();
include('includes/config.php');

error_reporting(0);


if(strlen($_SESSION['login'])==0)
{   
header('location:index.php');
}
else{
date_default_timezone_set('Asia/Kolkata');// change according timezone
$currentTime = date( 'd-m-Y h:i:s A', time () );
if(isset($_POST['submit']))
{
$sql=mysqli_query($con,"SELECT * FROM  students where 
pincode='".trim($_POST['pincode'])."' && 
StudentRegno='".$_SESSION['login']."'");
$num=mysqli_fetch_array($sql);

if($num>0)
{
 //statement for button

$_SESSION['pcode']=$_POST['pincode'];


if($status == "1") {
header("location:enroll.php"); //when system is on 
}
else {
    header("location:close.php"); //when system is off
}
}
else
{
$_SESSION['msg']="Error :Wrong Pincode. Please Enter a Valid Pincode !!";
}
}
?>

2 个答案:

答案 0 :(得分:1)

单击单选按钮,会将包含其属性的内容的字符串发送到PHP脚本。

如何从广播按钮获取数据

class ExampleState extends State<Example> {
   String title = "text text text text texwtxtwtwxtx wtxwtxwtwtxt";
   String description = "textexxtett t exte tx tetexxt text text text";

   @override
   Widget build(BuildContext context) {
      return Scaffold(
             body: SingleChildScrollView(
                   child: ConstrainedBox(
                          constraints: BoxConstraints(
                          minHeight: MediaQuery.of(context).size.height,
                          ),
                          child: Column(
                                 mainAxisSize: MainAxisSize.min, 
                                 children: [
                                           ConstrainedBox(
                                           constraints: BoxConstraints(
                                           minHeight: MediaQuery.of(context).size.height * 0.15,
                                           maxHeight: MediaQuery.of(context).size.height * 0.30,
              ),
                                           child: Container(
                                                  padding: EdgeInsets.fromLTRB(25, 0, 25, 25),
                                                  child: SingleChildScrollView(child: Column(
                                                         children: [
                                                     Text("$title\n"),
                                                  Text("$description")
                                                  ]))))]))));}},

无线电(类似于复选框)在未选择任何选项时不会将任何内容发送到PHP脚本。

您可以这样做:

<input type="radio" name="status" value= "1" >on<br/><br/>
<input type="radio" name="status" value= "0">off<br/><br/>

如果您不熟悉我在代码中使用的 ?? ,请查看null coalescing operator。如果您的PHP版本是<7.0,则可以改用ternary operator

答案 1 :(得分:-1)

我对ajax不太熟悉,但是您可以执行以下操作。只需认为存在一个名为statues_of_registration_form的字段,并且此字段默认为signup_confirm_table中的0(已禁用)。

然后在管理页面中,您可以让管理员更改状态。

  

admin.php

<form action="action.php" method="post">
 <label> Change the visibility of the signup page</label>
   <select name="change_visibility">
    <option value="1">Show</option>
    <option value="0">Hide</option>
   </select>
  <button type="submit" name="submit">Confirm</button>     
</form>

<?php
if(isset($_POST['submit']){

  $sql = "UPDATE signup_confirm_table SET statues_of_registration_form = '".$_POST['change_visibility']."';
  $result= $dbconnection->query($sql);

   if(isset($result)){

    echo "<script>
    alert('Form status updated');
    window.location='admin.php';
    </script>";
}else{
    if(mysqli_errno($con)){
        echo "Error:".mysqli_error($con);
    }  
 }

 }
?>

现在,管理员可以将statues_of_registration_form字段设置为01。接下来,您在signup.php中检查页面加载之前的状态,如下所示。

  

signup.php

<html>
<head></head>
<body>
<?php
 $sql = "SELECT statues_of_registration_form FROM signup_confirm_table";
 $result= $dbconnection->query($sql);
 $row = mysql_fetch_array($result);

 if( $row['statues_of_registration_form'] == 1 ) { ?>
 //if status of the form = 1 put your form here
  <form>
   <input type="text"/>
  </from>
 <?php 
}else{
  echo "<script>
        alert('Registration form disabled by the admin.');
        window.location='signup.php';
        </script>";
?>
</body>
</html>

请注意,这仅是示例。强烈建议您在sql中使用准备好的声明。希望您可以从中获得一些帮助。谢谢