[SOLVED] Using Select boxes to input date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

on your form script, when you view the source through your browser you will see you have 3 form fields....one for year, one for month, one for day.

each of these have a unique name.

When you submit the form, each of these values gets passed to your next script, which is when you manipulate the data before inserting it into your DB.

if you called the function by doing the followinf in your form script

Code: Select all

<? DateSelector("lastcontact_",""); ?>
When you submit this form you will have 3 values

Code: Select all

$_POST['lastcontact_Year'];
$_POST['lastcontact_Month'];
$_POST['lastcontact_Day'];
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Ok I see, when I view through the browser I can see the 3 form fields - now I feel like im getting somewhere in understanding. ok, as I have called the function in this way:

Code: Select all

<? DateSelector("dateoflastcontact_",""); ?>
3 Values are now being passed to my insert script

Code: Select all

$_POST['dateoflastcontact_Year'];
$_POST['dateoflastcontact_Month'];
$_POST['dateoflastcontact_Day'];
ok stage one complete.

Now to stage 2 which is manipulating the 3 values into 1 value and inserting them.
heres what I currently have in my insert script:

Code: Select all

<?php/* Connecting, selecting database */$link = mysql_connect("xxxxx", "xxx", "xxxx")   or die("Could not connect : " . mysql_error());mysql_select_db("contact_management_system",$link) or die("Could not select database");  $sql = "INSERT INTO people (person_id, salutation, firstname, surname, organisation, role, address1, address2, city, postcode, telephone, mobile, fax, dateoflastcontact, datecontactagain, notes, email, org_id )  VALUES ('$person_id','$salutation','$firstname','$surname', '$organisation', '$role', '$address1', '$address2', '$city', '$postcode', '$telephone', '$mobile', '$fax', '$dateoflastcontact', '$datecontactagain', '$notes', '$email','$org_id')"; $result = mysql_query($sql,$link) or die ( mysql_error($link));; header ("location: http://www.soi.city.ac.uk/organisation/p
now this is the point I get confused at.my mind just goes totally blank, trying to think of a way to do this.

ok so what I could do is create 3 variables

Code: Select all

<?php 
$var1 = "dateoflastcontact_Year"
$var2 ="dateoflastcontact_Day"
$var3 ="dateoflastcontact_Month"
?>
would that be ok?

I know I have to join the variables using the '.'.....

but how do I then link this to the $dateoflastcontact variable which is inserted into the database.

Mark thank you for your patience I Feel I have learnt so much rather than someone just giving me the answer which doesnt help at all.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$var1 = "dateoflastcontact_Year";
$var2 = "dateoflastcontact_Day";
$var3 = "dateoflastcontact_Month";


$dateoflastcontact = $var1."-".$var3."-".$var2;
it is now ready to insert using your current script
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Mark,

This is what ive done:

Code: Select all

<?php
/* Connecting, selecting database */
$link = mysql_connect("xxx", "xxx", "xxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = "dateoflastcontact_Year";
$var2 = "dateoflastcontact_Day";
$var3 = "dateoflastcontact_Month";  
$dateoflastcontact = $var1."-".$var3."-".$var2;

  $sql = "INSERT INTO people (person_id, salutation, firstname, surname, organisation, role, address1, address2, city, postcode, telephone, mobile, fax, dateoflastcontact, datecontactagain, notes, email, org_id ) 
 VALUES ('$person_id','$salutation','$firstname','$surname', '$organisation', '$role', '$address1', '$address2', '$city', '$postcode', '$telephone', '$mobile', '$fax', '$dateoflastcontact', '$datecontactagain', '$notes', '$email','$org_id')";
 $result = mysql_query($sql,$link) or die ( mysql_error($link));;
 header ("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
2 problems firstly when I click submit I now just get a blank screen and secondly the data does not insert into the ddatabase.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

sorry, my bad

Code: Select all

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month]";


$dateoflastcontact = $var1."-".$var3."-".$var2;
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

sorry now it wont add anything to the database and the screen still goes blank.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

oK Mark this is what ive got now but the result is that the record or the date will not be inserted into the database before I added the $_POST's I still got the data to insert minus the date - now neither of them will insert and I get a blank screen.

heres the code.

Code: Select all

<?php
/*Connecting, selecting database*/
$link = mysql_connect("xxxx", "xxxx", "xxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month]";  
$dateoflastcontact = $var1."-".$var3."-".$var2;

  $sql = "INSERT INTO people (person_id, salutation, firstname, surname, organisation, role, address1, address2, city, postcode, telephone, mobile, fax, dateoflastcontact, datecontactagain, notes, email, org_id ) 
 VALUES ('$person_id','$salutation','$firstname','$surname', '$organisation', '$role', '$address1', '$address2', '$city', '$postcode', '$telephone', '$mobile', '$fax', '$dateoflastcontact', '$datecontactagain', '$notes', '$email','$org_id')";
 $result = mysql_query($ sql, $link) or die ( mysql_error($link));;
 header ("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

any more tips on this?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what is everything echoing out as?

Code: Select all

$sql = '
    INSERT INTO
        people
            (person_id, salutation, firstname,
             surname, organisation, role, address1,
             address2, city, postcode, telephone,
             mobile, fax, dateoflastcontact,
             datecontactagain, notes, email, org_id)
    VALUES
            ("'.$person_id.'","'.$salutation.'","'.$firstname.'",
             "'.$surname.'" "',.$organisation.'", "'.$role.'",
             "'.$address1.'", "'.$address2.'", "'.$city.'",
             "'.$postcode.'", "'.$telephone.'", "'.$mobile.'",
             "'.$fax.'", "'.$dateoflastcontact.'",
             "'.$datecontactagain.'", "'.$notes.'", "'.$email.'",
             "'.$org_id.'")
';

$result = mysql_query($sql) or die ( mysql_error().__LINE__);
if that does not work it will give you a error message that it dies on so you will see it, if not then i really dont know what to tell you

ps. always use good spacing so other people can read it clearly, also (i was told) use ony a single quote (') around your query for security reasons (maybe this russian was stupid?)
AISBERG
Forum Newbie
Posts: 8
Joined: Sat May 21, 2005 3:17 pm
Location: Bucharest
Contact:

Post by AISBERG »

reply to first message in this topic. i think this is the simpliest method to fill those inputs.

Code: Select all

<?php
     $month=array(1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

   $days=range(1,31);
   $year=range(1990,2020);
   echo '<form action="calendar.php" method="post">';
   echo '<select name="month">';
   foreach($month as $key=>$value) {
       echo "<option value='$key'>$value</option>\n";
   }
   echo '</select>';

   echo '<select name="day">';
   foreach($days as $value) {
       echo "<option value='$value'>$value</option>\n";
   }
   echo '</select>';

   echo '<select name="year">';
   foreach ($year as $value) {
       echo "<option value='$value'>$value</option>\n";
   }
   echo '</select></form>';
?>
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Hi, let me share my code here:

Code: Select all

function DateSelectionBoxes($date='',$name='date'){
  if($date=='')
   $date = date("Y-m-d");
  $date_array = explode("-",$date);
  $yr = $date_array[0];
  $mn = $date_array[1];
  $dy = $date_array[2];
  setype($yr,'int');
  settype($mn,'int');
  settype($dy,'int');
  $lastday = 31;
  if(checkdate($mn,31,$yr)) 
    $lastday = 31;
  elseif(checkdate($mn,30,$yr)) 
    $lastday = 30;
  elseif(checkdate($mn,29,$yr)) 
    $lastday = 29;
  elseif(checkdate($mn,28,$yr)) 
    $lastday = 28;

  $str_year = "<input class=box type='text' name='".$name."_year' size=1 maxlength=4 value='".$yr."'>";
  $str_day = "<select class=box name='".$name."_day'>";
  for($i=1;$i<=$lastday;$i++){
    $str_day .= "<option value=".$i;
    if($i==$dy)
      $str_day .= " selected";
    $str_day .= ">".$i."</option>";
  }
  $str_day .= "</SELECT>";
  $str_month = "
  <SELECT name='".$name."_month' class=box>
  <option value=1".($mn==1?' selected':'').">January
  <option value=2".($mn==2?' selected':'').">February
  <option value=3".($mn==3?' selected':'').">March
  <option value=4".($mn==4?' selected':'').">April
  <option value=5".($mn==5?' selected':'').">May
  <option value=6".($mn==6?' selected':'').">June
  <option value=7".($mn==7?' selected':'').">July
  <option value=8".($mn==8?' selected':'').">August
  <option value=9".($mn==9?' selected':'').">September
  <option value=10".($mn==10?' selected':'').">October
  <option value=11".($mn==11?' selected':'').">November
  <option value=12".($mn==12?' selected':'').">December
  </SELECT>";

  return $str_month." ".$str_day.", ".$str_year;
}
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Shiznatix,pimptastic, I tried what you said and I still get the blank screen and no error message telling me which line is at fault, ok guys I dont seem to be getting anywhere and I was determined to get this done. heres my final attempt, i.e the two scripts involved.

Firstly my form script

Code: Select all

<h1>Administration</h1>
<?php
/*** Function: DateSelector** 
Input: STRING inName, INTEGER useDate** Output: ** 

Description: Creates three form fields for get month/day/year*/ function 
DateSelector($inName, $useDate)    
{  $monthName = array(1=>"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December");                

if($useDate == "")        
{ 
$useDate = time();        
}        
print("<SELECT NAME=\"" . $inName . "Month\">\n");        
for($currentMonth = 1; $currentMonth <= 12; $currentMonth++)       
{            
echo "<OPTION VALUE=\"";           
echo intval($currentMonth);           
echo "\"";           
if(intval(date("m", $useDate))==$currentMonth)            
{                
echo " SELECTED";           
}           
echo ">".$monthName[$currentMonth]."\n";       
}       
echo "</SELECT>";               
echo "<SELECT NAME=".$inName."Day>\n";       
for($currentDay=1; $currentDay <= 31; $currentDay++)        
{           
echo "<OPTION VALUE=\"$currentDay\"";           
if(intval(date("d", $useDate))==$currentDay)            
{                
echo " SELECTED";           
}           
echo ">$currentDay\n";       
}       
echo "</SELECT>";                   
echo "<SELECT NAME=".$inName."Year>\n";       
$startYear = date("Y", $useDate);        
if($startYear < 1997)        
{           
$startYear = date("Y");       
}                
for($currentYear = $startYear-1; $currentYear <= $startYear+2;$currentYear++)        {           
echo "<OPTION VALUE=\"$currentYear\"";           
if(date("Y", $useDate)==$currentYear)           
{                
echo " SELECTED";            
}           
echo ">$currentYear\n";        
}    echo "</SELECT>";}
?>

<?php
/* Connecting, selecting database */
$link = mysql_connect("xxxxx", "xxxx", "xxxxx")
   or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
?>



<body onLoad="focus()"> 

<form method="post" action="processpeople.html">

<table width="100%"  border="0">

    <tr> 
      <td width="17%"><font face="Times New Roman, Times, serif"><strong>Salutation</strong></font></td>
      <td width="27%"><font face="Times New Roman, Times, serif"> 
        <select name="salutation" style="color: #000000; 
			background-color: #ADD8E6">
          <option>Mr</option>
          <option>Mrs</option>
		  <option>Ms</option>
          <option>Miss</option>
		  <option>Prof</option>
		  <option>Dr</option>
        </select>
		
        </font></td>
      <td width="27%"><font face="Times New Roman, Times, serif"><strong>Telephone</strong></font></td>
      <td width="29%"><input name="telephone" type="text"style="color: #000000; 
background-color: #ADD8E6" size="20" maxlength="20">
</td>
    </tr>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>First Name</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="firstname" type="text"  size="20"style="color: #000000; 
		background-color: #ADD8E6">
        </font></td>
      <td><font face="Times New Roman, Times, serif"><strong>Mobile</strong></font></td>
      <td><input name="mobile" type="text"style="color: #000000; 
			background-color: #ADD8E6" size="20" maxlength="20"></td>
    </tr>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>Surname</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="surname" type="text" size="15"style="color: #000000; 
			background-color: #ADD8E6">
        </font></td>
     <td><font face="Times New Roman, Times, serif"><strong>Fax</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="fax" type="text"style="color: #000000; 
			background-color: #ADD8E6" size="20" maxlength="20">
        </font></td>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>Organisation</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="organisation" type="text" size="20"style="color: #000000; 
			background-color: #ADD8E6">
      <td><font face="Times New Roman, Times, serif"><strong>E-mail</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="email" type="text"style="color: #000000; 
			background-color: #ADD8E6" size="25" maxlength="50">
        </font></td>
    </font></tr>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>Role</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="role" type="text" size="25"style="color: #000000; 
		background-color: #ADD8E6">
        </font></td>
      <td><font face="Times New Roman, Times, serif"><strong>Date of Last Contact</strong></font></td>
      <td><font face="Times New Roman, Times, serif">
	  <?php DateSelector("dateoflastcontact_",""); ?>
        </font></td>
		</tr>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>Address (1)</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="address1" type="text"  size="20" style="color: #000000; 
		background-color: #ADD8E6">
        </font></td>
      <td><font face="Times New Roman, Times, serif"><strong>Date Contact Again</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <?php DateSelector("datecontactagain_",""); ?>
        </font></td>
    </tr>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>Address(2) </strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="address2" type="text"  size="20"style="color: #000000; 
		background-color: #ADD8E6">
        </font></td>
      <td><font face="Times New Roman, Times, serif"><strong>OID</strong></font></td>
      <td><font face="Times New Roman, Times, serif">
        <input name="org_id" type="text"style="color: #000000; 
			background-color: #ADD8E6" size="5">
        </font></td>
    </tr>
    <tr> 
      <td><font face="Times New Roman, Times, serif"><strong>City</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="city" type="text"  size="20"style="color: #000000; 
		background-color: #ADD8E6">
        </font></td>
      <td><font face="Times New Roman, Times, serif">&nbsp;</font></td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td height="43"><font face="Times New Roman, Times, serif"><strong>Post 
        Code</strong></font></td>
      <td><font face="Times New Roman, Times, serif"> 
        <input name="postcode" type="text" size="7"style="color: #000000; 
			background-color: #ADD8E6">
        </font></td>
      <td><font face="Times New Roman, Times, serif">&nbsp;</font></td>
      <td>&nbsp;</td>
    </tr>
  </table>
<input type="submit" name="submit" value = "Enter Information"> 
</form>





<p>
And once I click submit the data entered in the above form is passed onto my insert script which inserts the data.

Code: Select all

<?php
/*Connecting, selecting database*/
$link = mysql_connect("xxx", "xxxxx", "xxxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month]";  
$dateoflastcontact = $var1."-".$var3."-".$var2;


 $sql = "INSERT INTO people (person_id, salutation, firstname, surname, organisation, role, address1, address2, city, postcode, telephone, mobile, fax, dateoflastcontact, datecontactagain, notes, email, org_id ) 
 VALUES ('$person_id','$salutation','$firstname','$surname', '$organisation', '$role', '$address1', '$address2', '$city', '$postcode', '$telephone', '$mobile', '$fax', '$dateoflastcontact', '$datecontactagain', '$notes', '$email','$org_id')";
 $result = mysql_query($ sql, $link) or die ( mysql_error($link));;

 header ("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
this had been working perfectly well before and I could always go back to my previous file as I have a copy of it to get it working again.

Its just I want this dateselector function to insert dates correctly. this is where Im having problems, can anyone take a look please especially Pimpatastic as he was initially giving me advice.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

firstly, there are still some mistakes in your code!

this line has the closing quote in the wrong place!!

Code: Select all

$var3 = $_POST["dateoflastcontact_Month]";
There are 2 mistakes in this line.

Code: Select all

$result = mysql_query($ sql, $link) or die ( mysql_error($link));;
Remove one of the semi-colons!!

Remove the space between the $ and sql.

I know you said you aren't a good prgrammer, but this really is the kind of thing you should be spotting on your own.


Secondly, you have 2 date selectors on your previous form and you are only handling one!

Need to add the following

Code: Select all

$var1 = $_POST["datecontactagain_Year"];
$var2 = $_POST["datecontactagain_Day"];
$var3 = $_POST["datecontactagain_Month"];  
$datecontactagain = $var1."-".$var3."-".$var2;


If doing the above doesn't solve the problem...

Add

Code: Select all

echo $sql;
before the header call, then run the script. It should echo out the query that is trying to be executed. i wanna see the query that is being executed.

Then post the result back here.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Pimptastic Ive corrected all those errors and I still got the same result a blank screen with no data added, I also put in <?php echo $sql ?> before the header call and got no feedback - am I going nuts here or is nothing working?

Code: Select all

<?php
/*Connecting, selecting database*/
$link = mysql_connect("xxxxx", "xxxxx", "xxxxx")
   or die("Could not connect : " . mysql_error());
mysql_select_db("contact_management_system",$link) or die("Could not select database");

$var1 = $_POST["dateoflastcontact_Year"];
$var2 = $_POST["dateoflastcontact_Day"];
$var3 = $_POST["dateoflastcontact_Month"];  
$dateoflastcontact = $var1."-".$var3."-".$var2;

$var1 = $_POST["datecontactagain_Year"];
$var2 = $_POST["datecontactagain_Day"];
$var3 = $_POST["datecontactagain_Month"];  
$datecontactagain = $var1."-".$var3."-".$var2;



  $sql = "INSERT INTO people (person_id, salutation, firstname, surname, organisation, role, address1, address2, city, postcode, telephone, mobile, fax, dateoflastcontact, datecontactagain, notes, email, org_id ) 
 VALUES ('$person_id','$salutation','$firstname','$surname', '$organisation', '$role', '$address1', '$address2', '$city', '$postcode', '$telephone', '$mobile', '$fax', '$dateoflastcontact', '$datecontactagain', '$notes', '$email','$org_id')";
 $result = mysql_query($sql, $link) or die ( mysql_error($link));
echo $sql;
 header ("location: http://www.soi.city.ac.uk/organisation/ ... eople.html");
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

errrr.....aren't you submiting the form to the wrong place?!

Code: Select all

<form method="post" action="processpeople.html">

..and there are still mistakes in your code :cry:

...same ones i just told you about...double check them, then check them again...when you think you've finished checking...double check them again

EDIT: i take it back, you have edited your post :wink:
Last edited by JayBird on Wed May 25, 2005 4:50 am, edited 1 time in total.
Post Reply