Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Evaluating as FALSE... why?

    Code:
    <?php 
    	// This page is setup as an example of one page form processing
    	require_once("connections/widgetcorp.php");
    	require_once("includes/functions.php");
    	include("includes/header.php");
    
    	
    	// VALIDATE PASSED SUBJECT ID
    	if(intval($_GET['subj'] == 0)) {			// intval returns 0 if not an integer
    		redirect_to("content.php");
    	}
    	
    	// must test if form has been submitted on single page form processing
    	if (isset($_POST['submit'])) {
    
                        .
                        .
                        .
    
                        <input type="submit" name="submit" value="Edit Subject" />
                        <!-- the name="submit" will allow the name submit to be passed as a POST value-->
                    </form>
                    <br>
    				<a href="content.php">Cancel</a>
                </td>
            </tr>
        </table>
    </div>           
                 
    <?php
    	require("includes/footer.php"); 
    ?>

    I am very new to PHP programming and usually will try to find my bugs without help but on this I am stumped. I am wondering why the isset($_POST['submit'])) evaluates to FALSE after the Edit Subject button is pressed and the page reloads. Also, how would I then correct this so that it either evaluates as true or use another submission test?

    Thanks for your help.


    ~ grinnZ ~

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Show your complete code this may help us help you.

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Complete Code


    Code:
    <?php 
    	// This page is setup as an example of one page form processing
    	require_once("connections/widgetcorp.php");
    	require_once("includes/functions.php");
    	include("includes/header.php");
    
    	
    	// VALIDATE PASSED SUBJECT ID
    	if(intval($_GET['subj'] == 0)) {			// intval returns 0 if not an integer
    		redirect_to("content.php");
    	}
    	
    	// must test if form has been submitted on single page form processing
    	if (isset($_POST['submit'])) {
    		validate_form_input();
    	}
    
    	// SANITIZE INPUT
    	$id=mysql_prep($_GET['subj']);
    	$subject_name = mysql_prep($_POST['subject_name']);
    	$position = mysql_prep($_POST['position']);
    	$visible = mysql_prep($_POST['visible']);
    	
    
    	include("includes/header.php");
    
    	$id_table=get_sel_row_id();
    	$sel_id=$id_table[0];
    	$table=$id_table[1];
    	$sel_row=get_row_by_id($sel_id,$table);
    
     ?>
    
     <div id="main">
        <table id="structure">
            <tr>
                <td id="navigation">
                    <?php echo navigation(); ?>
    			</td>
    			<td id="page">
    				<h2>Edit Subject: <?php echo $sel_row['subject_name']; ?></h2>
                    <form action="edit_subject.php?subj=
    						<?php echo urlencode($sel_row['subject_name']); ?>" method="post">
                    	<p>Subject Name:  <input type="text" name="subject_name" 
                        	value="<?php echo $sel_row['subject_name']; ?>" id="subject_name" /></p>
                        <p>Position:  
                        	<select name="position">
                            	<?php
    								// populates position pulldown
    								$rstSubjects=get_all_subjects();
    								$intSubjCount=mysql_num_rows($rstSubjects);
    								// intSubjCount + 1 because we are adding a subject
    								for($count=1; $count <= $intSubjCount + 1; $count++) {
    									echo "<option value=\"{$count}\"";
    									if($sel_row['position']==$count) {
    										echo " selected";
    									}
    									echo ">{$count}</option>";
    								}
    							?>
                            </select>
                        </p>
                    	<p>Visible:  
                        	<input type="radio" name="visible" value="0"
                            	<?php 
    								if($sel_row['visible']==0) {
    									echo " checked";
    								}  ?> 
    						/> No
                            &nbsp;
                            <input type="radio" name="visible" value="1"
                            	<?php 
    								if($sel_row['visible']==1) {
    									echo " checked";
    								}  ?>
    						/> Yes
                        </p>
                        <input type="submit" name="submit" value="Edit Subject" />
                        <!-- the name="submit" will allow the name submit to be passed as a POST value-->
                    </form>
                    <br>
    				<a href="content.php">Cancel</a>
                </td>
            </tr>
        </table>
    </div>           
                 
    <?php
    	require("includes/footer.php"); 
    ?>

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Post the code for the validate_form_input() function.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    How do you know that if(isset($_POST['submit'])) is false? The posted code worked for me and reached the validate_form_input() function call.

    Have you done a "view source" of the form in your browser so that you know it is complete HTML and free of php errors?

    Perhaps validate_form_input(); is not doing what you think?

    What exactly do you see in front of you and what do you expect to see?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    function validate_form_input() {
    	$aryErrors=array();
    	
    	$req_fields=array('subject_name','position','visible');
    	foreach ($req_fields as $fieldname) {
    		if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
    			$aryErrors[]=$fieldname . " either unset or empty";	// $array[] will place string at last array position
    		}
    	}
    	
    	$fields_with_length=array('subject_name' => 30);
    	foreach ($fields_with_length as $fieldname => $maxlength) {
    		if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
    			$aryErrors[]=$fieldname . " has too many characters.";
    		}
    	}
    }
    
    
    function TFtest($expression) {
    	if($expression==TRUE) {
    		return "TRUE";
    	} else { 
    		return "FALSE";
    	}
    }
    Above you will find the validate_form_input function along with a quick function to test the value of isset($_POST['submit']). To answer what I see I will direct you here >> http://grinnz.servehttp.com:8089/gri...rp/content.php The edit_subject page is loaded from this page by clicking one of the non-bulleted items in the left sidebar (About, Products, Services, etc.) which will display the database record ready for editing on the edit_subject page. The false you see above the header was generated by test added just above the if(isset in question.

    Code:
    <?php 
    	// This page is setup as an example of one page form processing
    	require_once("connections/widgetcorp.php");
    	require_once("includes/functions.php");
    	
    	// VALIDATE PASSED SUBJECT ID
    	if(intval($_GET['subj'] == 0)) {			// intval returns 0 if not an integer
    		redirect_to("content.php");
    	}
    	
    	// on single page form processing must test if form has been submitted 
    	echo TFtest(isset($_POST['submit']));
    	if (isset($_POST['submit'])) {
    To produce the error edit the subject name and press Edit Subject. Instead of isset($_POST['submit']) testing true on the page reload to proceed with form validation it evaluates as false and returns to the content.php page.

    Finally, thank you all for your assistance.


    ~ grinnZ ~

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    The posted code with the TFtest() function works for me and echos TRUE. Something your code or your system is doing is causing the problem. I suspect that one of your include/require files is either overwriting the value or you have register_globals on and a same name variable is overwriting the value.

    Though not directly related to your problem, your validate_form_input() function sets $aryErrors[] but does nothing with it, nor does the function return it or a value for the main code to use.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Checking my php.ini I find that register_globals=off.

    Checking include/requires:
    require_once("connections/widgetcorp.php");
    .... Establishes the database connection and database select.

    include("includes/header.php");
    .... Displays page header. HTML only.

    require("includes/footer.php"); // at end
    .... Displays footer. HTML only. Also closes database connection.

    The only thing left is are the functions. Below you will find the complete functions file.

    Also, I do realize that the array in the validation function goes nowhere. For now I am working on the isset issue and will tackle the return of the array in the future.


    ~ grinnZ ~


    Code:
    <?php
    
    // DATABASE ==============
    
    function get_all_subjects() {
    	// returns everything from Subjects as recordset array
    	$qrySubjects="
    		SELECT * FROM subjects
    		ORDER BY position ASC";
    	$rstSubjects=qry($qrySubjects); 
    	return $rstSubjects;
    }
    
    function get_pages_for_subject($subject_id) {
    	// returns all records from pages for corresponding subject as recordset array
    	// used in sidebar menu construction
    	global $connection;
    	$qryPages="
    		SELECT * FROM pages 
    		WHERE subject_id={$subject_id} 
    		ORDER BY position ASC";
    	$rstPages=qry($qryPages); 
    	return $rstPages;
    }
    
    function get_row_by_id($id,$table) {
    	// returns single row from Subjects or Pages based on menu item clicked
    	// used in sidebar menu construction
    	if ($id==NULL) {
    		return NULL;
    	} else {
    		$qry="SELECT * ";
    		$qry.="FROM " . $table . " ";
    		$qry.="WHERE id=" . $id . " ";
    		$qry.="LIMIT 1";
    		
    		$rst=qry($qry);
    		if ($row=mysql_fetch_array($rst)) {
    		// note:
    		// althought the fetch_array could be successful it could also return no result
    		// so if no rows are returned then fetch_array would return FALSE
    		// if ($row==true) {
    			return $row;
    		} else
    			return NULL;
    	}
    }
    
    function get_sel_row_id() {   //(find_selected_page)
    	// used for sidebar menu construction
    	global $sel_subj_id;
    	global $sel_page_id;
    	
    	if(isset($_GET['subj'])) {					// subject clicked
    		$sel_subj_id=$_GET['subj'];			
    		$sel_page_id=NULL;
    	} elseif (isset($_GET['page'])) {		// page clicked
    		$sel_subj_id=NULL;
    		$sel_page_id=$_GET['page'];
    	} else {											// neither clicked (page first opened)
    		$sel_subj_id=NULL;
    		$sel_page_id=NULL;
    	}
    	
    	if ($sel_subj_id<>"") {
    		$table="subjects";
    		$sel_id=$sel_subj_id;
    	} else {
    		$table="pages";
    		$sel_id=$sel_page_id;
    	}
    
    	return array($sel_id,$table);
    }
    
    function qry($qry) {
    	global $connection;
    	$rst=mysql_query($qry,$connection); 
    	test_query($rst);
    	return $rst;
    }
    
    function test_query($rst) {
    	if (!$rst) {
    		die("Database query failed: " . mysql_error()) . "<br />";
    	}
    }
    
    
    
    // FORMS  ==============
    
    function mysql_prep($value) {
    	// cleans values for insertion to the database by mysql
    	$magic_quotes_active=get_magic_quotes_gpc();
    	$compat_php=function_exists("mysql_real_escape_string");		// PHP >= 4.3.0
    	if($compat_php) {																	// PHP >= 4.3.0
    		//undo any magic quotes effects; allow mysql_real_escape_string changes
    		if($magic_quotes_active) {
    			$value=stripslashes($value);
    		}
    		$value=mysql_real_escape_string($value);
    	} else {
    		// if magic quotes inactive then manually add slashes
    		// else magic quotes are active and the slashes already exist
    		if (!$magic_quotes_active) {
    			$value=addslashes($value);
    		}
    	}
    	return $value;
    }
    
    function validate_form_input() {
    	$aryErrors=array();
    	
    	$req_fields=array('subject_name','position','visible');
    	foreach ($req_fields as $fieldname) {
    		if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
    			$aryErrors[]=$fieldname . " either unset or empty";	// $array[] will place string at last array position
    		}
    	}
    	
    	$fields_with_length=array('subject_name' => 30);
    	foreach ($fields_with_length as $fieldname => $maxlength) {
    		if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
    			$aryErrors[]=$fieldname . " has too many characters.";
    		}
    	}
    }
    
    
    
    // PROCESSING ==============
    
    function navigation() {
    	// main sidebar navigation menu construction routine
    	// returns string of all screen output
    	global $sel_subj_id;
    	global $sel_page_id;
    	global $sel_id;
    // PERFORM THE SUBJECTS QUERY 
    	$rstSubjects=get_all_subjects();
    	
    	// LIST THE SUBJECTS
    	$output = "<ul class=\"subjects\">";
    	while($subject=mysql_fetch_array($rstSubjects)) {
    		$li_calc=li_calc($subject["id"],$sel_subj_id);
    		$output .= "<li" . $li_calc . "<a href=\"edit_subject.php?subj=".
    			urlencode($subject["id"]) . "\">{$subject["subject_name"]}</a></li>";
    		// NOTE: inline catination... quotes only at start and end of $output .= string
    
    		// PERFORM THE PAGES QUERY 
    		$rstPages=get_pages_for_subject($subject["id"]);
    		
    		// LIST THE PAGES
    		$output .= "<ul class=\"pages\">";
    		while($page=mysql_fetch_array($rstPages)) {
    			$li_calc=li_calc($page["id"],$sel_page_id);
    			$output .= "<li" . $li_calc . "><a href=\"content.php?page=".
    				urlencode($page["id"]) . "\">{$page["page_name"]}</a></li>";								
    		}
    		$output .= "</ul>";		// close page list
    	}
    	$output .= "</ul>";			// close subject list
    	
    	return $output;
    }
    
    
    
    // UTILITIES ==============
    
    function redirect_to($location = NULL) {
    	// redirect to page action
    	if ($location != NULL) {
    		header("Location: {$location}");
    		exit;			// must have exit with header command
    	}
    }
    
    function li_calc($selsubj,$clicked_id) {
    	// returns class string for <ul> tag as necessary
    	// used for content display
    	$li="";
    	if($selsubj==$clicked_id) {
    		$li=" class=\"selected\"";
    	}
    	return $li;
    }
    
    function TFtest($expression) {
    	if($expression==TRUE) {
    		return "TRUE";
    	} else { 
    		return "FALSE";
    	}
    }
    
    ?>

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    The false you see above the header was generated by test added just above the if(isset in question.
    The FALSE you see on the edit page is because at the time that page is requested, the form on the edit page has not been submitted yet. The false value being shown has nothing to do with the problem.

    At your link, clicking the submit button on the "edit" page results in a redirect to the content.php page. Something in your code is causing that and I just found why -

    Have you done a "view source" of the form in your browser so that you know it is complete HTML and free of php errors?
    If you had viewed the source of your edit form, you will see that the GET ?subj parameter is being set to the name of the item being edited. In your code you do if(intval($_GET['subj'] == 0)). The intval() is converting a string name in to a false/zero, which the test matches and the redirect takes you to the content.php page.

    Also, not directly related to your problem, you need to put an exit; statement after every header() redirect to prevent the remainder of the code on the page from executing while the browser performs the redirect.

    Your page only works in FF, not IE. But you are using simple href links. That would indicate that the HTML is invalid and needs to be fixed. In looking at the source of the page, your opening <li tags are incomplete.
    Last edited by CFMaBiSmAd; 07-16-2008 at 12:48 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    grinnz (07-16-2008)

  • #10
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    To finally answer your question whether I have viewed the page source I can only confirm what you already know... I have not. As I am frankly only days into writing PHP I will take your advice and add that to my debugging arsenal. Also, thank you for pointing out that the initial <li> tags are unclosed. Again your source viewing advice is well noted.

    Regarding HTML, I too noticed just earlier that the page was broken in IE. Thank you for pointing me in the direction to scope that problem.

    Finally your determination that the subject was being changed by the intval to me was indispensable. With my current knowledge and understanding I would never have figured that out. I will work to find a different test routine that will be more reliable.

    Thank you again...


    ~ grinnZ ~



    hmmmm... now where's that button to thank this man? Don't help... I'll find it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •