Jump to content

need help validating php


dj262501

Recommended Posts

Hello all,

 

I need your help again. I have this page where the user inputs data:

 

<?php
include "include/dbc.php";

include "include/header.inc";

#error checking will go here
?>

<style type="text/css">
.mydate{
color:#00F;
text-decoration:underline;
cursor:pointer;
}
</style>

<script type="text/javascript">
function displayDate(d){
var date=new Date();
var D=date.getDate();
date.setDate(D+d);
var YYYY=date.getFullYear();
var MM=date.getMonth()+1;
MM<10?MM='0'+MM:null;
var DD=date.getDate();
DD<10?DD='0'+DD:null;
var span=document.getElementById('date');
span.innerHTML= 'Entries for '+MM+'/'+DD+'/'+YYYY;
}
onload=function(){displayDate(0)};
</script>

<h1>Food Diary</h1>

<div class="full">
<center><div><span class="mydate" onclick="displayDate(-1)"><img src="images/left_arrow.png" border="0">Yesterday</span>      <span id="date" style="font-size:2em;"></span>      <span class="mydate" onclick="displayDate(1)">Tomorrow<img src="images/right_arrow.png" border="0"></span></div><br />
<a href="#" onclick="displayDate(0);return false;">Today</a>
</center>

<div class="full">
<form name="exercise" id="exercise" method="GET" action="">
  <center><table>
<tr>
    	<td><h3>Add an Activity</h3></td>
</tr>
<tr>
    	<td><input name="NewSearchString" style="width: 100px" type="text"/>  <input type="submit" value="Search" /> </td>
</tr>
    <tr>
    	<td>
        	<select name="activity">
            	<option value="_">Activity Browse...</option>
                <option value="all">All Activities</option>
                <option value="biking">Biking</option>
                <option value="condition">Conditioning</option>
                <option value="dancing">Dancing</option>
                <option value="fish">Fishing & Hunting</option>
                <option value="Home">Home Activities</option>
                <option value="misc">Miscellaneous</option>
                <option value="music">Music Playing</option>
                <option value="occupation">Occupation</option>
                <option value="running">Running</option>
                <option value="sports">Sports</option>
                <option value="walking">Walking</option>
                <option value="water">Water Activities</option>
                <option value="winter">Winter Activities</option>
		</select>   <input type="submit" value="Submit" /></td></tr></table></center></form>
	</td>
</tr>
</table> </center>
<table width="100%">
   	<tr bgcolor="#66CC33">
    	<td><div>Activity</div></td>
        <td><div>Specific Activity</div></td>
        <td><div>Time (hh:mm)</div></td>
        <td><div>Distance</div></td>
        <td><div>Units</div></td>
</tr>
    <tr bgcolor="#66CC33">
    	<td><div></div></td>
     	<td><div></div></td>
       <td><div></div></td>
        <td><div class="Float"></div></td>
        <td class="cp_Distance"><div></div></td>
</tr>
<?php
if(isset($_GET[activity])) {
$category=$_GET[activity];
$result = mysql_query("SELECT * FROM exercise WHERE type='$category'");
?>
<form action="add_activity.php" method="POST">
<?php
while($row = mysql_fetch_array($result)) {
echo '<tr><td><div>'.$row[Type].'</div></td>';
echo '<td><div>'.$row[Name].'<input type="hidden" name="exerciseid[]" value="'.$row[Name].'"></div></td>';
echo '<td><div><input type="text" name="duration['.$row['Name'].']"></div></td>';
echo '<td><div><input type="text" name="distance['.$row['Name'].']"></div></td>';
echo '<td><div><select name="metric[]">
			<option value=""></option>
        		<option value="mile">mile</option>
            	<option value="Km">km</option>
            	<option value="M">m</option>;
            	<option value="Yard">yrd</option>
            	<option value="Feet">ft</option>
            </select></div></td></tr>';
echo'<input type="hidden" name="submitted" value="1">';

}
mysql_close();
?>

<tr><td colspan="6" align="center"><input type="submit" name="submit" value="Add Activities" onClick="return confirm(
  'Are you sure you want to submit the activities?');"></td></tr>
</form>
<?php
}
?>
    <tr bgcolor="#66CC33">
    	<td><div></div></td>
     	<td><div></div></td>
       <td><div></div></td>
        <td><div class="Float"></div></td>
        <td class="cp_Distance"><div></div></td>
</tr></table>      
<div></div>
<?php include "include/footer.inc"; ?>

 

I want to validate that the user enters data for at least one of all the fields provided (distance, duration, metric). If someone could help me get started I'd greatly appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/214572-need-help-validating-php/
Share on other sites

So only one of the three form fields you listed need a value, but the other 2 may have a value, right? When you validate the fields just make sure that all three aren't empty. Make sense, or did I misinterpret what you're trying to do?

 

if( $_POST['submitted'] == 1 ) {
     if( empty($_POST['distance']) && empty($_POST['duration']) && empty($_POST['metric']) ) {
          // No fields have a value
     } else { 
          // at least one of the fields has a value
     }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.