duanjuan1103 2016-03-22 02:37
浏览 32

检查表单值以用作条件而无需提交表单

I have been using this code:

<html>
<head>
<script>
function validatePfin() {
var x1 = document.forms["pfin"]["cash"].value;
var x2 = document.forms["pfin"]["all_assets"].value;
var x3 = document.forms["pfin"]["all_debt"].value;

if (x1 == null || x1 == "") {alert("Please enter Cash Amount.");}
else {
if (x2 == null || x2 == "") {alert("Please enter Non-Cash Assets.");}
else {
if (x3 == null || x3 == "") {alert("Please enter Total Debt.");}

else { 
 document.getElementById('sub1').value='Processing, please wait ...   '; 
 document.getElementById('sub1').disabled=true; 
form.submit(); 

 }}}

return false;

}
</script>
 </head>
<body>
<table>
<form method="POST" name="pfin"  autocomplete="on" target="vframe" action="p_financials_s.php" enctype="multipart/form-data" onsubmit="return validatePfin()">

<tr><td>
<select name="cash" id="cash">
<option value="<?php echo $row['cash'];?>" selected><?php echo $row['cash'];?></option>
<option value="">Select</option>
<option value="0" >0</option>
<option value="1000" >1000</option>
</select>
<tr><td>

<select name="all_assets" id="all_assets">
<option value="<?php echo $row['all_assets'];?>" selected><?php echo $row['all_assets'];?></option>
<option value="">Select</option>
<option value="0" >0</option>
<option value="1000" >1000</option>
</select>    
<tr><td>

<select name="all_debt" id="all_debt">
<option value="<?php echo $row['all_debt'];?>" selected><?php echo $row['all_debt'];?></option>
<option value="">Select</option>
<option value="0" >0</option>
<option value="1000" >1000</option>
</select>
<tr><td>
<input type="submit" id="sub1" class="back1" value="Save And Go To Next Step >">&nbsp;
</table>
  </form>
 </body></html>

This is a pared down snip from the code. What it does is that upon submission, the form checks to see if the fields have values and if not stops the submission.

What I am trying to figure out is how, using PHP/javascript (or ?), I can 'read' a form field, check to see if it has a value, and make that a variable for a conditional statement, w/o having to submit and post to the DB.

For example, let's say I want the "cash" value to have a value, and if that is true, then I can make that a condition to, let's say, display a link:

<? 
  $cash1 = form.field.not-yet-sumbitted.cash
 if( $cash1 != "") {
   echo "http://examplesite.com";
 }
?>

I have tried using an onchange event in javascript to somehow bring the "cash" variable into memory, and then once done, somehow have the PHP variable pick it up, but that doesn't fire.

  • 写回答

2条回答 默认 最新

  • duansha6410 2016-03-22 02:43
    关注

    Since you are submitting the form as POST to p_financials_s.php, on that file you can simple get the value of any field from the global $_POST array. e.g.

    $field1 = $_POST['field1'];
    

    and then you can simply add a check on that field. You can add validation on javascript end if you just want to validate things and then submit the form.

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line