AdRock Posted September 14, 2007 Share Posted September 14, 2007 This is really starting to do my head cos I can't see what is wrong I have a function that fills the value field of a form with 2 possible variables but they are not getting set so when you view source you just see value="" I also try echoing these variables to see if they are getting set but they are not. Is it becuase they need to be set to global? echo $oldvalue."<br>"; echo $newvalue."<br>"; echo $get_first_name."<br>"; echo $get_username."<br>"; echo $get_interests."<br>"; If someone could look through the code and see why those variables aren't getting set it would help me out a lot <?php if (!is_authed()) { print ('You need to login to view this page, <a href="index.php?page=login">click here</a> to login.'); } else { echo "<h2>Edit Profile - ".$_SESSION['username']."</h2><hr />"; if(isset($_POST['submit'])) { $submit = $_POST['submit']; } $result = mysql_query("SELECT `first_name`, `last_name`, `username`, `interests` FROM `users` WHERE `username`='".$_SESSION['username']."'"); while($row = mysql_fetch_assoc($result)) { $get_first_name = $row['first_name']; $get_last_name = $row['last_name']; $get_username = $row['username']; $get_interests = $row['interests']; } $oldvalue= "old old value"; $newvalue= "new new alue"; function error_field($error, $field) { if($error[$field]) { echo $oldvalue; } else { if($print_again) { echo $newvalue; } else { echo $oldvalue; } } } function check_form() { global $_POST, $error, $print_again; global $first_name, $last_name, $username, $newsletter; $first_name = check_input($_POST['first_name']); $last_name = check_input($_POST['last_name']); $interests = check_input($_POST['interests']); $username = check_input($_POST['username']); $newsletter = $_POST['newsletter']; $error['first_name'] = false; if(empty($first_name)) { $error['first_name'] = true; $print_again = true; $message="<li>The <span><b>Forename</b></span> field is empty</li>"; } else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) { $error['first_name'] = true; $print_again = true; $old_value = $first_name; $message="<li><span><b>Forename</b></span> must contain letters only</li>"; } if(empty($last_name)) { $error['last_name'] = true; $print_again = true; $message.="<li>The <span><b>Surname</b></span> field is empty</li>"; } else if(!ereg("^[A-Za-z\-]{2,30}$",$last_name)) { $error['last_name'] = true; $print_again = true; $message.="<li><span><b>Surname</b></span> must contain letters only</li>"; } if(empty($username)) { $error['username'] = true; $print_again = true; $message.="<li>The <span><b>Username</b></span> field is empty</li>"; } else if( mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) ) { $error['username'] = true; $print_again = true; $message.="<li>The username you have selected has already been used by another member in our database. Please choose a different Username!</li>"; } else if(!ereg("^[A-Za-z0-9 \-]{4,30}$",$username)) { $error['username'] = true; $print_again = true; $message.="<li><span><b>Username</b></span> must contain letters and numbers only</li>"; } if($print_again) { echo "<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p> <ul id=\"validation\">$message</ul>"; show_form(); } else { // Stop the form being used from an external URL // Get the referring URL $referer = $_SERVER['HTTP_REFERER']; // Get the URL of this page $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]; // If the referring URL and the URL of this page don't match then // display a message and don't send the email. if ($referer != $this_url) { echo "You do not have permission to use this script from another URL.<br />"; echo "If you are behind a firewall please check your referrer settings."; exit; } //These just test to make sure what variables are getting passed echo $username."<br>"; echo $first_name."<br>"; echo $last_name."<br>"; echo $interests."<br>"; echo $oldvalue."<br>"; echo $newvalue."<br>"; echo $get_first_name."<br>"; echo $get_username."<br>"; echo $get_interests."<br>"; } } function show_form() { global $_POST, $print_again, $error; global $first_name, $last_name, $interests, $username, $newsletter; ?> <form method="post" id="login" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> <fieldset> <legend>Username</legend> <p class="hint">Please enter a username for your user account. Note that username should be between 4 and 30 characters.</p> <p><label for="username">Username:</label> <input type="text" <?php error_bool($error, "username"); ?> title="Please enter a username" id="username" name="username" size="30" value="<?php echo $username; ?>" /></p> </fieldset> <fieldset> <legend>About You</legend> <p><label for="first_name">Forename:</label> <input type="text" <?php error_bool($error, "first_name"); ?> title="Please enter your first name" id="first_name" name="first_name" size="30" value="<?php error_field($error, "first_name"); ?>" /></p> <p><label for="last_name">Surname:</label> <input type="text" <?php error_bool($error, "last_name"); ?> title="Please enter your last name" id="last_name" name="last_name" size="30" value="<?php echo $last_name; ?>" /></p> <p class="hint">Please enter any interests/hobbies you have (optional).</p> <p><label for="interests">Your Interests:</label> <input type="text" title="Please enter any interests/hobbies you have" id="interests" name="interests" size="30" value="<?php echo $interests; ?>" /></p> </fieldset> <fieldset> <legend>Newsletter</legend> <p class="hint">Sign up for jackgodfrey.org.uk newsletter. This newsletter includes what forthcoming events we are arranging, the latest Honeylands news and our latest news. You can Opt-out at any time....so sign up and give it a try!!</p> <p><label for="newsletter">Newsletter:</label> <input style="border:none" type="radio" value="yes" id="newsletter" checked="checked" name="newsletter" />Opt-in <input style="border:none" type="radio" value="no" name="newsletter" />Opt-out</p> </fieldset> <p><label for="Submit" style="width: 20px"> </label> <input type="submit" name="submit" value="Edit Profile" class="sendbutton" /> <input type="reset" value="Reset Fields" class="sendbutton" /></p> </form> <?php } if(isset($submit)) { check_form(); } else { show_form(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69325-variables-either-not-being-passed-between-functions-or-variables-not-being-set/ Share on other sites More sharing options...
wildteen88 Posted September 14, 2007 Share Posted September 14, 2007 Functions cannot use variables outside of their scope. functions have there own scope. if you want to use variables that are out side of the functions scope you'll need to set them as global within the function eg: function error_field($error, $field) { global $oldvalue, $newvalue; You have to list each individual variable to be global for every function that uses them. Quote Link to comment https://forums.phpfreaks.com/topic/69325-variables-either-not-being-passed-between-functions-or-variables-not-being-set/#findComment-348490 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.