佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 964|回复: 7

PHP 基础疑问,请各位高手帮帮我

[复制链接]
发表于 2-4-2008 09:59 PM | 显示全部楼层 |阅读模式
嗨!各位PHP高手,请帮帮我这位新手,好吗?谢谢!!!我的Code如下:

<?php
  session_start();
  $_SESSION['Name'] = $_POST['Name'];
  $_SESSION['sex'] = $_POST['sex'];
  $_SESSION['Code'] = $_POST['Code'];
    $_SESSION['good'] = $_POST['good'];
?>
<html>
<head>
<title>Testing</title>
<meta http-equiv="Content-Type" c>
<head>
<body>
<?php
        function CheckBoxValue($chekboxvalue) {
               
                if (isset($_POST["hobby1"])) {                        
               
                        $ArrayOfSelection = $_POST["hobby1"];        
                        
                        for ($x = 0; $x < count($ArrayOfSelection); $x++) {
                                
                                if ($ArrayOfSelection[$x] == $chekboxvalue) {
                                        echo ("CHECKED");
                                        break;
                                }
                        }
                        
                }        
        }
?>
<?php
if ($_POST['change'] == 'change') {
  if (isset($_POST['Hobby']) && !empty($_POST['Hobby'])) {
    $hobby = stripslashes($_POST['Hobby']);
    $hobby = unserialize($hobby);
  
    for($i=0;$i<3;$i++) {
      if ($hobby[$i]=='Gaming') {
        $temp = $hobby[$i];
        $hobby[$i] = $hobby[0];
        $hobby[0] = $temp;
      }
      if ($hobby[$i]=='Cooking') {
        $temp = $hobby[$i];
        $hobby[$i] = $hobby[1];
        $hobby[1] = $temp;
      }
      if ($hobby[$i]=='Swimming') {
        $temp = $hobby[$i];
        $hobby[$i] = $hobby[2];
        $hobby[2] = $temp;
      }
    }
    foreach($hobby as $key => $val) {
      if (empty($val)) {
        unset($hobby[$key]);
      }
    }
  }
}

if (isset($_POST['Code'])) {
$Code= $_POST['Code'];
settype($Code,integer);
}

if ($_POST['submit'] == 'submit')
{
if (($_POST["Name"]!="")&&($_POST["hobby1"]!="")&&($_POST["sex"]!="")&&($_POST["good"]!=""))
  {
  if(($_POST["Code"]!="")&&(ereg ("(^[1-9])",$Code)))
  {
  $Name=$_POST['Name'];
  $sex=$_POST['sex'];
  $good=$_POST['good'];
  
  if($_POST["hobby1"])
  {   
      foreach($_POST['hobby1'] as $value)
     {
        $hobby2=join (', ', $_POST['hobby1']);
  $test=count($_POST['hobby1']);
     }
     $serializeHobbies = serialize($_POST["hobby1"]);
  }
  echo
  ("  
  <fieldset style='border: 1px ridge #0000ff; background: #ffffff; width: 350px;'>
  <b>Name:</b>$Name <br>
  <b>Hobby:</b> $hobby2<br>
  <b>Sex:</b> $sex <br>
  <b>Goods Name</b>             <b>Quantity</b><br>
  <I>A.</I> $good                     $Code
  </fieldset></br>");
  
  echo
  ("<form name='frmtest' method = 'post' action='test.php'>
  <input type = 'hidden' name = 'Name' value = '$Name'>
   <input type = 'hidden' name = 'sex' value = '$sex'>
   <input type = 'hidden' name = 'Code' value = '$Code'>
   <input type = 'hidden' name = 'good' value = '$good'>
  <input type = 'hidden' name = 'Hobby' value = '$serializeHobbies'>
  <br>
   <input type = 'submit' name = 'change' value = 'change'><br><br>
    <input type = 'submit' name = 'submit' value = 'submit'>
  </form>");
  exit;
}
else
{
$error1="Quantity Cannot Empty/Only Number!!!";
}
}
else
{$error="Cannot Empty!!!";}
}
if ($_POST['submit'] == 'submit')
{
}
?>
<?php echo $error;?><?php echo $error1;?>
<form name="frmtest" method="post" action="test.php">
  <p>
    <strong>Name:</strong>
    <input type="text" name="Name" value="<?php echo $_SESSION['Name'] ?>">
    <br>
    <br>
    <strong>Hobby:</strong><br>
  <input name="hobby1[]" type="checkbox" value="Gaming" <?php CheckBoxValue("Gaming"); ?><?php if (isset($hobby[0])) echo "checked"; ?>>
  Gaming
  <br>
  <input name="hobby1[]" type="checkbox" value="Cooking" id="hobby1" <?php CheckBoxValue("Cooking"); ?><?php if (isset($hobby[1])) echo "checked"; ?>>
  Cooking
  <br>
  <input name="hobby1[]" type="checkbox" value="Swimming"<?php CheckBoxValue("Swimming"); ?> <?php if (isset($hobby[2])) echo "checked"; ?>>
  Swimming
  <p><strong>Sex:</strong><br>
    <input type="radio" name="sex" value="M"<?php echo (($_SESSION['sex'] == "M")?"CHECKED":""); ?>>
    Male
    <input type="radio" name="sex" value="F"<?php echo (($_SESSION['sex'] == "F")?"CHECKED":""); ?>>
    Female
  <table width="293" border="1">
    <tr>
      <td width="180" bgcolor="#FFFF00"><div align="center"><strong>Goods</strong></div></td>
      <td width="97" bgcolor="#FFFF00"><div align="center"><strong>Quantity</strong></div></td>
    </tr>
    <tr>
      <td>A.
      <input name="good" type="text" size="10" value="<?php echo $_SESSION['good'] ?>"></td>
      <td><input name="Code" type="text" size="3" maxlength="3" value="<?php echo $_SESSION['Code'] ?>"></td>
    </tr>
    <tr>
      <td>B.
      <input name="good2" type="text" id="good2" size="10"></td>
      <td><input name="Code2" type="text" size="3" maxlength="3"></td>
    </tr>
  </table>
  <p>
    <input type="submit" name="submit" value="submit">
  
</form>
</body>
</html>
回复

使用道具 举报


ADVERTISEMENT

发表于 2-4-2008 10:16 PM | 显示全部楼层

回复 1# mikeng 的帖子

你的问题是?????
回复

使用道具 举报

 楼主| 发表于 2-4-2008 10:24 PM | 显示全部楼层
问题一:我要如何才能够做到“Code”栏位没有输入时,错误讯息显示的同时栏位不会出现零且栏位上只可输入数目字?
问题二:我要如何才能够做到使用户顺序的输入资料?例如不可以不输入Row A 就跑去输入Row B。
问题三:我要如何检查用户一定要完成Row A;其次Row B 可以不输入。但Row B 一旦有资料在“good2”或“code2”里,要如何检查另外一个栏位一定要输入?
问题四:我要如何才能够做到<fieldset>排列整齐。例如当所有的资料输入正确时,按Submit钮,图框底线根据有多少Row而排列整齐。
回复

使用道具 举报

发表于 3-4-2008 11:08 AM | 显示全部楼层
1) 检查$_POST['Code']。如果是Empty string '',就不要settype($Code,integer); ,直接放empty string。
栏位只可放数字,你可以用php 的is_numeric,或用以下的function (integer only)
        function is_integer($input) {
                // Accept positive and negetive integer
                if (ereg("^-{0,1}[0-9]+$", $input))
                        return true;
                else
                        return false;
        }

2) 用javascript,在Row B加 onchange event。当user 在Row B 打字时,用javascript 检查 Row A 是否empty。如是,focus on Row A,然后Alert。
3) 在submit button加onclick event。检查Row B看看是不是一个又record,一个没有record。如是,alert user,然后 return false。
4) 不明白。。。。

其实如果你不要用Javascript 也行。就是每次Submit后,你必须逐一检查每个textbox 的record,做validation。如有不复合你的要求,就show error message,如果全都对,那才继续proceed。
回复

使用道具 举报

发表于 3-4-2008 11:49 AM | 显示全部楼层
试试找ajax framework。
回复

使用道具 举报

 楼主| 发表于 3-4-2008 09:08 PM | 显示全部楼层

回复 5# vampcheah 的帖子

谢谢您们的回复。可以用我的Code做个例子吗?
回复

使用道具 举报

Follow Us
发表于 4-4-2008 11:55 AM | 显示全部楼层
<?php
function intCheck($input) {
        // Accept positive and negetive integer
        if (ereg("^-{0,1}[0-9]+$", $input))
                        return true;
        else
                        return false;
}

if (isset($_POST)) {
        // something has been posted.
        $code = $_POST['Code'];
        $code2 = $_POST['Code2'];
        $good = $_POST['good'];
        $good2 = $_POST['good2'];
        $error = '';
       
        // Validate code
        if (trim($code) == '') {
                $error .= "Please enter quantity for Row A.\n";
        } else {
                // check if it's integer
                if (!intCheck($code)) {
                        $error .= "Quantity 1 is not a valid integer.\n";
                }
        }
       
        if (trim($good2) == '' && trim($code2) != '') {
                // fill in code 2, but not good2
                $error .= "Please enter quantity for Row B.\n";
        }
       
        if (trim($good2) != '' && trim($code2) == '') {
                // fill in good 2, but not code 2
                $error .= "Please enter good for Row B.\n";
        }
}

if ($error == '') {
        // No error. Proceed to process
        //...
        //....
}

?>
<html>
<head>
<title>Testing</title>
<meta http-equiv="Content-Type">
<script language="JavaScript" type="text/javascript">
function CheckA() {
        if (document.forms['frmtest'].good.value == '') {
                alert('Please enter Row A first !');
                document.forms['frmtest'].Code2.value = '';
                document.forms['frmtest'].good2.value = '';
                document.forms['frmtest'].good.focus();
                return false;
        }
       
        if (document.forms['frmtest'].Code.value == '') {
                alert('Please enter Row A first !');
                document.forms['frmtest'].Code2.value = '';
                document.forms['frmtest'].good2.value = '';
                document.forms['frmtest'].Code.focus();
                return false;
        }
}

function submitValidate() {
        if (document.forms['frmtest'].good.value == '' || document.forms['frmtest'].Code.value == '') {
                alert('Please enter Row A first !');
               
                if (document.forms['frmtest'].good.value == '') {
                        document.forms['frmtest'].good.focus();
                } else {
                        document.forms['frmtest'].Code.focus();
                }
               
                return false;
        } else {
                return true;
        }
}
</script>
</head>
<body>
<?php echo $error;?>
<form name="frmtest" method="post" action="test.php">
  <p>
    <strong>Name:</strong>
    <input type="text" name="Name" value="<?php echo $_POST['Name'] ?>">
    <br>
    <br>
    <strong>Hobby:</strong><br>
  <input name="hobby1[]" type="checkbox" value="Gaming">
  Gaming
  <br>
  <input name="hobby1[]" type="checkbox" value="Cooking" id="hobby1">
  Cooking
  <br>
  <input name="hobby1[]" type="checkbox" value="Swimming">
  Swimming
  <p><strong>Sex:</strong><br>
    <input type="radio" name="sex" value="M">
    Male
    <input type="radio" name="sex" value="F">
    Female
  <table width="293" border="1">
    <tr>
      <td width="180" bgcolor="#FFFF00"><div align="center"><strong>Goods</strong></div></td>
      <td width="97" bgcolor="#FFFF00"><div align="center"><strong>Quantity</strong></div></td>
    </tr>
    <tr>
      <td>A.
      <input name="good" type="text" size="10" value="<?php echo $good; ?>"></td>
      <td><input name="Code" type="text" size="3" maxlength="3" value="<?php echo $code; ?>"></td>
    </tr>
    <tr>
      <td>B.
      <input name="good2" type="text" id="good2" size="10" onKeyDown="CheckA();" value="<?php echo $good2; ?>"></td>
      <td><input name="Code2" type="text" size="3" maxlength="3" onKeyDown="CheckA();" value="<?php echo $Code2; ?>"></td>
    </tr>
  </table>
  <p>
    <input type="submit" name="submit" value="submit" onClick="return submitValidate();">
</form>
</body>
</html>

简化版本。看看是你要的效果吗?
回复

使用道具 举报

 楼主| 发表于 4-4-2008 08:44 PM | 显示全部楼层

回复 7# yhchan 的帖子

谢谢您!我先试试看。
回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 31-12-2025 06:38 PM , Processed in 0.120996 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表