Lodius2000 Posted June 25, 2008 Share Posted June 25, 2008 in the following script for some reason my global vars ($id,$title, and probably $db(dont know how to test it)) dont work in process_form(). process form, upon submission only prints the link to go back, and I need it to make a couple sql queries and then print the link <?php //this page edits articles require ('../../../install/PEAR/DB.php'); require ('../../../../dbfiles/db_login.php'); require ('../requires/formhelpers.php'); require ('../requires/timearrays.php'); $db->setErrorHandling(PEAR_ERROR_DIE); $db->setFetchMode(DB_FETCHMODE_ASSOC); //grabs article_id for later query $id = $_GET['id']; if ($_SESSION['username']){ if($_POST['_submit_check']){ if($form_errors = validate_form()){ show_form($form_errors); } else { process_form(); } } else { show_form(); } } else { print '<a href="../index.php">Log In dummy</a>'; } function show_form($errors = '') { global $db, $id; $items = $db->getRow('SELECT article_title, article FROM text_cms WHERE article_id = ?', array($id)); $title = stripslashes($items['article_title']); $article = stripslashes($items['article']); //If form has been submitted, get defaults from submitted variables if ($_POST['_submit_check']){ $defaults = $_POST; } else { $defaults = array('title' => $title, 'body' => $article); } if ($errors){ print 'Please correct these errors: <ul><li>'; print implode('</li><li>', $errors); print '</li></ul>'; } print '<form name="edit" method="POST" action="'.htmlentities($_SERVER['PHP_SELF']).'">'; //begin the unique form print '<br />'; print "Article Title"; print "<br />\n"; input_title('title', $defaults); print "<br /><br />\n"; print 'Article Body'; print "<br />\n"; input_textarea('body', $defaults); print "<br /><br />\n"; input_submit('submit', 'Edit Article'); print '<input type="hidden" name="_submit_check" value="1" />'; print "\n"; print '</form>'; } function validate_form(){ if (trim(strlen($_POST['title'])) > 255){ $errors[] = 'Article title is too long, please shorten it'; } return $errors; } function process_form(){ global $db, $id, $title; $edited_title = mysql_real_escape_string($_POST['title']); $edited_article = mysql_real_escape_string($_POST['body']); //normally there would be sql statements here but i changed it to just test the global vars //regardless, all of the vars inside process_form() here are all that will be inside the function when it actually works print $id; print $title; print '<a href="../managearticle/index.php">Go back to article management</a>'; } ?> Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/ Share on other sites More sharing options...
kenrbnsn Posted June 25, 2008 Share Posted June 25, 2008 Instead of using the global statement, just pass the values you need to the function. You already do that with some of the variables, why not all of them. Ken Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-574484 Share on other sites More sharing options...
Lodius2000 Posted June 25, 2008 Author Share Posted June 25, 2008 I know it is inside a function so i could just pass the values to the variables, but the variables are already defined elsewhere in the script and they work fine, i dont understand why I would need to redefine them instead of just using global Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-574491 Share on other sites More sharing options...
Lodius2000 Posted June 25, 2008 Author Share Posted June 25, 2008 edit to last post, also $db cant be redefined unless i can put a require inside a function Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-574496 Share on other sites More sharing options...
kenrbnsn Posted June 25, 2008 Share Posted June 25, 2008 When you pass variables to a function they are not redefined. For example: <?php function test1($a,$b,$c="This is variable C") { echo "---------------------<br>\n"; echo '$a = ' . $a . "<br>\n"; $a = "A different value"; echo '$b = ' . $b . "<br>\n"; $b = 'Another different value'; echo '$c = ' . $c . "<br>\n"; echo "---------------------<br>\n"; } $a = 'This is $a'; $b = 'This is $b'; $c = 'This is $c'; test1($a,$b,$c); echo '$a is now <span style="font-weight:bold">' . $a . "</span><br>\n"; echo '$b is now <span style="font-weight:bold">' . $b . "</span><br>\n"; echo '$c is now <span style="font-weight:bold">' . $c . "</span><br>\n"; test1('A different value for $a','Something else'); echo '$a is still <span style="font-weight:bold;color:red">' . $a . "</span><br>\n"; echo '$b is still <span style="font-weight:bold;color:red">' . $b . "</span><br>\n"; echo '$c is still <span style="font-weight:bold;color:red">' . $c . "</span><br>\n"; ?> As you can see if you run the above code, the values of $a, $b and $c do not change outside the function. You need to read up on the scope of variables. Ken Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-574507 Share on other sites More sharing options...
Lodius2000 Posted June 25, 2008 Author Share Posted June 25, 2008 Ken, this still doesnt answer why i can use global $db, $id; in show_form(), but the same line in process_form() doesnt work all I want to do is use the same variables with the same values in 2 functions but for some reason php wont let me, this is not the first time that i have used the same vars in 2 functions but it is the first time that it doenst work Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-574511 Share on other sites More sharing options...
kenrbnsn Posted June 25, 2008 Share Posted June 25, 2008 I took your code, striped out everything that wasn't relevant,put in some debugging "echos" and ran it. It ran fine. Are you sure you're even getting to the process_form() function call? This line: <?php if($form_errors = validate_form()){ ?> is bothering me because, whether or not it detects errors, the "if" will evaluate as "true" because the assignment succeeds and you will never get to the process_form() call. Ken Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-574526 Share on other sites More sharing options...
Lodius2000 Posted June 26, 2008 Author Share Posted June 26, 2008 ken, I solved the problem, the problem is, upon form submission the GET portion of the url disapeared so $id wasnt working anymore, which didnt allow my query in show form to work since it was dependent on the id number it kinda all clicked in my head at once, after 3 solid weeks of staring at it now I know Link to comment https://forums.phpfreaks.com/topic/111928-solved-why-dont-my-global-vars-persist-in-process_form/#findComment-575479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.