Solved

php if problem

Posted on 2011-03-20
28
270 Views
Last Modified: 2012-05-11
I am a newbe struggling with placing a php variable into an html form.  As written, the result produces the same action if "true" or "false"

I am trying to direct the form-tag "action" based on the php variable.

Can someone assist in helping to get this to work?
__________________________________

<?php
include("eTicketVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");


$search=$_POST['search'];

$data = 'SELECT * FROM `Ticket` WHERE `ID` = "'.$search.'"';
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
  $data2 = mysql_fetch_array($query);

$App_date = "$Approv_date";
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$Approval = strtotime($App_date);

if ($Approval > $today) {
     $act = "edit_eTicket2-1.php";
} else {
     $act = "eTick_approved.htm";
}
   
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><meta http-equiv="Content-Language" content="en-us">
<TITLE>Test eTicket Worksheet</TITLE>
<BASE target="main">
<script type="text/javascript" src="_js/main_tw.js"></script>
</head>
<body onload="init()">
<script type="text/javascript" src="_js/menu_tw.js"></script>
<blockquote>
      <form name="edit_eTicket1" method="POST" action="$act">
______________________________

Thanks for any assistance,

Dixon
0
Comment
Question by:dibrandt
  • 12
  • 12
  • 4
28 Comments
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
Change the last line into this:

      <form name="edit_eTicket1" method="POST" action="
<?php
echo $act;
?>
"

0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
Oops, forgot the closing ">":

      <form name="edit_eTicket1" method="POST" action="
<?php
echo $act;
?>
">
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
That would be the way.  Note that you have this <script type="text/javascript" src="_js/main_tw.js"></script> in there twice when once in the <head> should be enough.
0
 

Author Comment

by:dibrandt
Comment Utility
This is great!

DaveBaldwin:  I believe that one is “main_tw.js” and the other is “menu_tw.js”.

AriMc:  It seems that the variable is now going to the "action=", however I am still getting the same page if "$act" is True or False.  

Do I have a problem in the structure of the "IF"?
0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
Are you sure your database query is correct?

It look to me like this:

   $App_date = "$Approv_date";

shoud be something like

   $App_date = $data2['Approv_date'];

(assuming your table Ticket has a column named Approv_date)
0
 

Author Comment

by:dibrandt
Comment Utility
AriMc:  Actually, I am not sure.  

The table Ticket does in fact have a column named Approv_date.

I changed the query as you suggested - same result.

Should it be

       $App_date = $data2[Approv_date];

In other calls for column data from

         $data2 = mysql_fetch_array($query);

the format the works is as I have shown above ( no tick marks).

0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
You can try both, PHP often accepts quite strange formats. I'd put, and always do put, the ticks around the column name though.

0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
You could also debug the script by echoing the variable values just before you if test, like this:

echo "Approval: ".$Approval."<br>\n";
echo "Today: ".$today."<br>\n";

if ($Approval > $today) {

Then reload the page and see what the values just before the test are.

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Array names should be quoted in PHP to avoid confusion.  Look for "Array do's and don'ts" in this page: http://us3.php.net/manual/en/language.types.array.php
0
 

Author Comment

by:dibrandt
Comment Utility
DaveBaldwin: read with interest.  Does this also apply to PHP 5.0 with MySql 5.0?

AriMc: attempted to debug as suggested, got the following error message:

         Parse error: syntax error, unexpected $end in /htdocs/edit_eTicket1-2.php on line 118

line 118:  </HTML>

If this were Microsoft, I would accept that error as a normal message however, . . .
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Yes, it applies to all versions of PHP.  When PHP reads the row from MySQL (and other databases), it puts it in an array.
0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
Without the complete source code it's hard to say what the problem on line 118 is, but
in my earlier example I meant that you should only add these two lines:

   echo "Approval: ".$Approval."<br>\n";
   echo "Today: ".$today."<br>\n";

... and add them just before the if test that was already there:

   if ($Approval > $today) {

The error sounds like you added an additional "if test" that was present in my instruction. Please check that and remove the additional line if present. Then post us what you see on the reloaded page's additional lines, something like:

   Approval: xxxx
   Today: 2011-03-20 ...

0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
"Parse error: syntax error, unexpected $end in /htdocs/edit_eTicket1-2.php on line 118" usually means that one of your functions or blocks (if, while, etc...) wasn't 'closed', didn't have the ending '}'.
0
 

Author Comment

by:dibrandt
Comment Utility
DaveBaldwin: I thought that the PHP statement was good for all versions, just wanted to be sure.

Thanks for the insight on the error, however I am getting blury eyed looking at it.

AriMc: I verified the insert and location of the "echo", here's the code:


_____________________

<?php

include("eTicketVars.inc");

$con = mysql_connect($host, $user,$passwd);
mysql_select_db($database, $con)or die ("Couldn't select database.");


$search=$_POST['search'];

$data = 'SELECT * FROM `Ticket` WHERE `ID` = "'.$search.'"';
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
  $data2 = mysql_fetch_array($query);

$App_date = $data2['Approv_date'];
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$Approval = strtotime($App_date);

echo "Approval: ".$Approval."<br>\n";
echo "Today: ".$today."<br>\n";

if ($Approval >= $today) {
     $act = "edit_eTicket2-2.php";
} else {
     $act = "eTick_approved.htm";
     
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><meta http-equiv="Content-Language" content="en-us">
<TITLE>Bobwhite eTicket Worksheet</TITLE>
<BASE target="main">
</head>
<blockquote>
      <form name="edit_eTicket1-2" method="POST" action="<?php echo $act;?>">
      
<p align="center">    <font face="Arial" size="1">W&nbsp;&nbsp;&nbsp; O&nbsp;&nbsp;&nbsp;    O&nbsp;&nbsp;&nbsp;    D&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B&nbsp;&nbsp;&nbsp;    A&nbsp;&nbsp;&nbsp;    D&nbsp;&nbsp;&nbsp;    G&nbsp;&nbsp;&nbsp; E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; F&nbsp;&nbsp;&nbsp; O&nbsp;&nbsp;&nbsp; R&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T&nbsp;&nbsp;&nbsp; H&nbsp;&nbsp;&nbsp; E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp; 1&nbsp;&nbsp; st&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C&nbsp;&nbsp;&nbsp; E&nbsp;&nbsp;&nbsp; N&nbsp;&nbsp;&nbsp; T&nbsp;&nbsp;&nbsp; U&nbsp;&nbsp;&nbsp;     R&nbsp;&nbsp;&nbsp; Y</font></p>
<HR>

<table border="0" cellpadding="0" cellspacing="0" width="90%" height="20">
      <tr>
            <td><b><font size="6">Wood Badge Ticket Work Sheet</font></b><br>
            <font size="4">A Personal Statement of Vision and Mission</font></td>
            <td width="158">
            <IMG width="150" height="121" src="images/WB-LOG.gif" alt=Log align="right" ></td>
      </tr>
</table>
<p></p>
<p><font size="2" face="Arial">ID Number: <input type="text" name="ID" value="<?php echo $data2[ID]?>" size="4" tabindex="1" readonly="true">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Date:(mm/dd/yyyy) <input type="text" name="Date" value="<?php echo $data2[Date]?>" size="11" tabindex="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Date Stamp:
<input type="text" name="Time" value="<?php echo $data2[Time]?>" size="16" tabindex="3"></font><font size="2" face="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font>

<p><font face="Arial"><font size="2">Name: <input type="text" name="Name" value="<?php echo $data2[Name]?>" size="35" tabindex="3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Wood Badge Course No.: C7-129-11-01</font></font></P>
<p><font size="2" face="Arial">My Scouting Position:
<input type="text" name="ScoutingPosition" value="<?php echo $data2[ScoutingPosition]?>" size="37" tabindex="4"></font></P>
<p><font size="2" face="Arial">The Team That Will Benefit From My Leadership:
<input type="text" name="Team" value="<?php echo $data2[Team]?>" size="82" tabindex="5"></font></P>
<p><font face="Arial"><B>Scouting's Value</B></font></p>
<blockquote>
      <blockquote>
            <font face="Arial"><b><font size="2">Scout Oath<br></font></b>
            <font size="2">On my honor I will do my best to do my duty to God and my country and to obey the Scout Law; to help other people at all times; to keep myself physically strong, mentally awake, and morally straight.</font><br><br>

            <font face="Arial"><b><font size="2">Scout Law</font></b><br>
            <font size="2">A Scout is trustworthy, loyal, helpful, friendly, courteous, kind, obedient, cheerful, thrifty, brave, clean, and reverent.</font></font></font></blockquote>
</blockquote>
<p><font face="Arial"><B>Scouting's Mission Statement </B></font>&nbsp;</p>
<blockquote>
      <blockquote>
            <p><font face="Arial" size="2">The mission of the Boy Scouts of America is to prepare young people to make ethical and moral choices over their lifetimes by instilling in them the values of the Scout Oath and Law.</font></P>
      </blockquote>
</blockquote>

<P><font face="Arial"><B>My vision of success and the related plan of action</B><br>

<textarea rows= 9 name="MyVision" size="100" tabindex="6" style="text-align: left" cols="89"><?php echo $data2[MyVision]?></textarea>
</font></P>

<p><font face="Arial" size="2"><b>SMART Goal </b>(Specific, Measurable, Attainable,
Relevant, and Timely)</font><br>
<textarea rows=8 name="Smart"  size="96" tabindex="7" style="text-align: left" cols="89"><?php echo $data2[Smart]?></textarea></font></p>
<p><font face="Arial" size="2"><b>Who:</b><br><textarea rows=4 name="Who" size="96" tabindex="8" style="text-align: left" cols="89"><?php echo $data2[Who]?></textarea></font></p>

<p><font face="Arial" size="2"><b>What:</b><br><textarea rows=4 name="What" size="96" tabindex="9" style="text-align: left" cols="89"><?php echo $data2[What]?></textarea></font></p>

<p><font face="Arial" size="2"><b>When:</b><br><textarea rows=4 name="WhenD" size="96" tabindex="10" style="text-align: left" cols="89"><?php echo $data2[WhenD]?></textarea></font></p>

<p><font face="Arial" size="2"><b>Where:</b><br><textarea rows=4 name="WhereD" size="96" tabindex="11" style="text-align: left" cols="89"><?php echo $data2[WhereD]?></textarea></font></p>

<p><font face="Arial" size="2"><b>Why:</b><br><textarea rows=4 name="Why" size="96" tabindex="12" style="text-align: left" cols="89"><?php echo $data2[Why]?></textarea></font></p>

<p><font face="Arial" size="2"><b>How:</b><br><textarea rows=4 name="How" size="96" tabindex="13" style="text-align: left" cols="89"><?php echo $data2[How]?></textarea></font></p>

<p><font face="Arial" size="2"><b>How Verified:</b><br><textarea rows=4 name="HowVerified" size="96" tabindex="14" style="text-align: left" cols="89"><?php echo $data2[HowVerified]?></textarea></font></p>





            <p></P>
            <p><input type="submit" value="Submit" name="B1"></p>      
            </form><HR>
      <H5>Wood Badge eTicket Worksheet.<br>Copyright © 2009
Northeast Illinois Council. All rights reserved. <BR>Revised:
      <!--WEBBOT BOT=TimeStamp
    S-Type="EDITED"
    S-Format="%m/%d/%y"
--></H5>
</blockquote>
</BODY>
</HTML>

___________________
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dibrandt
Comment Utility
AriMc:  Additional note, there were no error messages prior to the addition of the "echo" statement.

I did "copy paste" the statement into my code, but I could have hit an extra key in the process.
0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
At some point you seem to have lost one closing curly bracket here:

   if ($Approval >= $today) {
        $act = "edit_eTicket2-2.php";
   } else {
        $act = "eTick_approved.htm";
   }   /* <-- THIS ONE IS MISSING */
   ?>
0
 

Author Comment

by:dibrandt
Comment Utility
AriMc: That got the "echo" however, the echo data implies that a "True" condition exist.  My logic is off.

Give me a couple of minutes to adjust, and test again.

Approval: 1298869200
Today: 1300593600
0
 

Author Comment

by:dibrandt
Comment Utility
AriMc: I reversed the statement condition (from xxx >= xxxx to xxxx <= xxxx) and the IF statement is still selecting the "True" condition.

The echo indicates that $Approval is less then Today.  
____________

if ($Approval <= $today) {
     $act = "edit_eTicket2-2.php";
} else {
     $act = "eTick_approved.htm";
}    

__________________

under this condition $act should be "False" and "Action=" should be "eTick_approved.htm"

What am I missing?
0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
That sounds strange. When I do a simplified simulation of your code I get expected results:

Can you try this short code in your environment in test.php for example?
Approval: 1298869200
today: 1300593600
2
1

Open in new window

<?php

$Approval = 1298869200;
$today = 1300593600;

echo "Approval: ".$Approval."<br>\n";
echo "today: ".$today."<br>\n";

if ($Approval >= $today) {
        echo "1<br>";
} else {
        echo "2<br>";
}

if ($Approval <= $today) {
        echo "1<br>";
} else {
        echo "2<br>";
}

?>

Open in new window

0
 

Author Comment

by:dibrandt
Comment Utility
AriMc:  I ran the test code snip with the following results:

Approval: 1298869200
today: 1300593600
2
1

The basic IF statement in php (my environment) seems to be working correctly.  Is there a problem when the variable string is passed to html?  How would I test for that?
0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
Are you sure you are looking at the correct place in your produced HTML. Your form name is still the same (edit_eTicket1-2) in both cases, just the action should change:

<form name="edit_eTicket1-2" method="POST" action="<?php echo $act;?>">

0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
Also, have you tried emptying your browser's cache between thr tries?

0
 

Author Comment

by:dibrandt
Comment Utility
AriMc: The form statement:
       <form name="edit_eTicket1-2" method="POST" action="<?php echo $act;?>">

I deleted all temp files, cookies, and history from the browser.

The Action= value was "edit_eTicket2-2.php"  

The Action= value should be  "eTick_approved.htm"

I changed the IF statement to:

if ($Approval <= $today) {
     $act = "eTick_approved.htm";
} else {
     $act = "edit_eTicket2-2.php";
}    

Cleared the browser.  The same results.  
0
 
LVL 9

Expert Comment

by:AriMc
Comment Utility
That's really strange. As a last method I've usually deleted the strange lines completely retyped them manually. Sometimes that has fixed it. In these cases I've assumed the lines to have contained some invisible control characters that can be interfere the execution.

0
 

Author Comment

by:dibrandt
Comment Utility
AriMc:

I will rekey the script, and let you know how it worked.
0
 

Author Comment

by:dibrandt
Comment Utility
AriMc:
I have rekeyed the scripted, no joy!

There was a change in the IF statement, now it provides a True result reguardless of the actual value.

Could this be accomplished with a "Switch"?
0
 
LVL 9

Accepted Solution

by:
AriMc earned 500 total points
Comment Utility
I copied your whole source code, added the one missing curly bracket and replaced the database reading lines with constant assignments:

    $Approval = 1298869200;
    $today = 1300593600;

Then changed the if condition between

    if ($Approval <= $today) {

and

    if ($Approval >= $today) {

And I get perfectly logical results when looking at the page source code:

   <form name="edit_eTicket1-2" method="POST" action="edit_eTicket2-2.php">

and

   <form name="edit_eTicket1-2" method="POST" action="eTick_approved.htm">

I'm sorry but I'm all out of ideas on what to try next, since the basic if-tests in your environment seem to work just fine.

0
 

Author Closing Comment

by:dibrandt
Comment Utility
Was not able to arrive at a complete working solution, but a lot of time and effort went into the result.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now