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 >">
</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.
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.
What you're aiming for is best done through AJAX calls in JavaScript due to their client-sided nature and ability. PHP is a server-sided language meaning that without submitting the <form>
(to itself at the very least) there would be no way for PHP to interpret it.
A common methodology is to implement the jQuery library's $.ajax()
functionality, effectively allowing you to POST
or GET
information to/from a PHP script without having to leave the page or submit the form itself, such as through onclick
calls.
Examples can be found in documentation here (towards the bottom of of the document): http://api.jquery.com/jquery.ajax/.