2017-03-08 19 views
0

我有一個從數據庫獲取信息的頁面,並且應該在提交時更新信息。我使用「switch」語句,但它不能識別提交按鈕的值。表格代碼位於「personal_change_form.inc」文件中。代碼如下。提前感謝您提供任何幫助。 personal_change.phpPHP switch語句無法識別提交按鈕

<?php 
/* Name: personal_change.php 
* Desc: This page updates information if there is a change 
*/ 
include("misc.inc"); 

session_start(); 
if(@$_SESSION['auth'] != "yes") 
    { 
     header("Location:user_login.php"); 
     exit(); 
    } 

    switch ($_POST['button']) 
{ 
    case "Update": 

$connect = mysqli_connect($host,$user,$password,$database) 
       or die("Couldn't connect to database."); 
$sql = "SELECT * FROM data WHERE loginName='{$_SESSION['logname']}'"; 
$result = mysqli_query($connect,$sql) 
     or die ("Couldn't execute query."); 
$row= mysqli_fetch_assoc($result); 
extract($row); 
if($value != $_POST['value']) 
    { 
    $sql = "INSERT INTO data (loginName,createDate,password,                 firstName,lastName,title,gender,course,sector,experience,qualific,location,mobile,email) VALUES          ('$_POST[loginName]', 
        '', 
        SHA1('$_POST[password]'), 
        '$_POST[firstName]', 
        '$_POST[lastName]', 
        '$_POST[Title]', 
        '$_POST[Gender]', 
        '$_POST[course]', 
        '$_POST[Sector]', 
        '$_POST[experience]', 
        '$_POST[qualific]', 
        '$_POST[Location]', 
        '$_POST[mobile]', 
        '$_POST[email]')"; 


        mysqli_query($connect,$sql) 
    or die("Couldn't execute query"); 
    header("location:personal.php"); 


    } 

    break; 
    default: 
    include("personal_change_form.inc"); 
} 


?> 

personal_change_form.inc

<?php 
/* Program: personal_change_form.inc 
* Desc:  This program displays information from the database 
*    and allow user to change it. 
*/ 
include("misc.inc"); 
$personal_change_array = array ("loginName" => "User Name:", 
        "password" => "Password:", 
        "firstName" => "First Name:", 
        "lastName" => "Last Name:", 
        "title"  => "Title:", 
        "gender" => "Gender:", 
        "course" => "Courses offered:", 
        "sector" => "Industry sector:", 
        "experience"=> "Trainer experience(years):", 
        "qualific" => "Academic qualifications:", 
        "location" => "Location\City:", 
        "mobile" => "Mobile:", 
        "email"  => "E-mail:"); 
?> 
<html> 
<head><title>Update Information</title></head> 
<style type="text/css"> 
<!-- 
    label {font-weight:bold; float:left; width:27%; 
    margin-right: .5em; text-align:right;} 
      fieldset {border:2px solid #000000 } 
    legend {font-weight:bold; font-size: 1.2em; 
      margin-bottom: 20px; text-align:center; padding-left:20px; padding-right:20px;border: 1px solid #888; 
    border-right: 1px solid #666; 
    border-bottom: 1px solid #666; 
    padding: 5px; 
    background-color: rgba(255,102,0,1.00)} 
    h3 {text-align: center; margin: 2em;} 
       #wrapper {margin:0; padding: 0;} 
       #login {position: absolute; left:0; width:40%; 
         padding: 1em 0; } 
       #reg {position:absolute; left:40%; width:60%; 
         padding: 1em 0;} 
       #field {padding-bottom: .5em} 
       .menu {margin-left:150px; padding-bottom: .5em; width:204px} 
       .errors {font-weight: bold; font-size:12px font-style:italic; 
         font-size:90%; color: red; margin-top:0; text-align:center} 
    --> 
    </style> 
<body> 
     <form action="<?php $_SERVER['PHP_SELF']?>" 
     method="POST"> 
     <fieldset><legend>UPDATE INFORMATION</legend> 

<?php 


echo "<table style='width:20%; left:25%; position:absolute; font-weight:bold' cellspacing='28.5'>\n"; 
    foreach($personal_change_array as $field=>$value) 
{ 
     echo "<tr><td>$value</td></tr>"; 
} 
echo "</table>"; 
echo "<table style='width:10%; left:25%; position:relative; font-weight:bold' cellspacing='15'>\n"; 
$connect = mysqli_connect($host,$user,$password,$database) 
       or die("Couldn't connect to database."); 
$sql = "SELECT * FROM data WHERE loginName='{$_SESSION['logname']}'"; 
$result = mysqli_query($connect,$sql) 
     or die ("Couldn't execute query."); 
$row= mysqli_fetch_assoc($result); 
foreach($row as $field => $value) 
{ 
    if($field!="createDate") 
    { 
     if($field=="password") 
     { 
      $type='password'; 
     } 
      else 
      { 
       $type='text'; 
      } 

echo "<tr><td><input ' type='$type' value=$value></input></td></tr>"; 

    } 
} 
echo "</table>"; 
?> 

    <input type="submit" name="button" 
     style='margin-left:40%; margin-bottom:.5em; margin-top:30px' value="Update" /> 

</fieldset></form> 
</body> 
</html> 
+0

我已經試過你的代碼,但即使按鈕不工作(不能點擊,不發送表格)。也許是因爲其他一些錯誤?例如,personal_change_form.inc中的行'echo「​​」;'有一個多餘的'符號,但是沒有任何域名。 否則你可以檢查提交按鈕的值,它應該工作。 –

+0

'sha1'不是一個安全的哈希。使用['password_hash'](http://php.net/manual/en/function.password-hash.php)和['password_verify'](http://php.net/manual/en/function.password- verify.php)散列密碼。 – apokryfos

+0

@GezaBoehm您提到的這行不是按鈕,它是表單輸入字段。按鈕行如下:

回答

0

按鈕名稱不提交的,所以PHP $ _POST [ '提交']值未設置。正如isset($ _ POST ['submit'])計算結果爲false。

您可以使用javascript函數來發布表單,通過爲按鈕提供id並在那裏您可以通過id獲取按鈕值,並將表單序列化並提交到所需的操作。

例如,你可以使用

<form id="my_awesome_form" action="<?php $_SERVER['PHP_SELF']?>" 
     method="POST"> 
</form> 
    <input id="submit_button" type="submit" name="button" 
     style='margin-left:40%; margin-bottom:.5em; margin-top:30px' value="Update" /> 
    <script> 
     var buttonVal = $('#submit_button').val(); 

     var form = $('#my_awesome_form').serialize(); 
    form.push({button: buttonVal}); 
     form.find('select:first').change(function() { 
     $.ajax({ 
      type: "POST", 
      url: form.attr('action'), 
      data: form.serialize(), 
      success: function(response) { 
      console.log(response); 
      } 
     }); 
     }); 
</script> 

現在使用Ajax調用數據提交表格併發送buttonVal到Ajax調用

+0

謝謝你的努力,但我對javascript不好。在我的代碼按鈕名稱是'按鈕',所以我用$ _POST ['按鈕'] –

+0

@ vicky92727我試過按鈕張貼,它效果很好。名稱/值對到達$ _POST超全局。您在哪個系統/服務器上遇到問題? –

+0

@GezaBoehm我錯了按鈕的值可以通過名稱和ID獲得,我的jquery解決方案是爲NahidMirzayer,因爲Nahid在獲取這些值時遇到問題,所以我建議嘗試jQuery。 – vicky92727