Solved

How to echo variables in a page outside of a function

Posted on 2016-09-16
5
86 Views
Last Modified: 2016-09-17
I have an edit form which should be showing users details in a form once they have logged in and want to edit their details. If I use code in the actual page itself I have no problem, but I have put everything in a function and can't use the variables inside the form. How can I do this?

The function:

function updateProfile($link){
	
	
	
	$sql = "SELECT user_firstname, user_lastname, user_email FROM `users` WHERE user_username = '{$_SESSION['username']}'";
		$result = $link->query($sql);
			if($result->num_rows === 1){
				while($row = $result->fetch_assoc()){
					
					$db_firstname = htmlentities($row['user_firstname']);
					$db_lastname = htmlentities($row['user_lastname']);
					$db_username = htmlentities($_SESSION['username']);
					$db_email = htmlentities($row['user_email']);
					$db_userid = $row['user_id'];
				}
			}

	
	$message = "";
	
	if(isset($_POST['updateprofile'])){
		
		if(empty($_POST['user_firstname'])) {
			
			$message .= "First name required<br>";
		}
		
		if(empty($_POST['user_lastname'])) {
			
			$message .= "Last name is required<br>";
		}
		
		if(empty($_POST['user_username'])) {
			
			$message .= "Username is required<br>";
		}
		
		if(empty($_POST['user_email'])) {
			
			$message .= "Email address required";
		}
		
		if(!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {

			$message .= "Email address invalid";
		}
		
		if(!empty($_POST['someinput'])) {
				
				$message .= "Be gone spam bot!";
			}
		
		if ($message) {
			
			$message = "<div class='alert alert-danger'><strong>There were errors in your form:<br></strong>" .$message. "</div>";
			
		} else {
			
			$user_firstname = $link->real_escape_string($_POST['user_firstname']);
			$user_lastname = $link->real_escape_string($_POST['user_lastname']);
			$user_username = $link->real_escape_string($_POST['user_username']);
			$user_email = $link->real_escape_string($_POST['user_email']);
			
			
			$update_profile = "UPDATE `users` SET user_firstname = '$user_firstname', user_lastname = '$user_lastname', user_username = '$user_username', user_email = '$user_email' WHERE user_id = '$db_userid'";
				
			if($result = $link->query($update_profile) === TRUE){
				
				$message = "<div class='alert alert-success>User profile updated</div>";
			}
			
		}
	
	}
	
	return $message;
	
	
}

Open in new window


The form:

<form action="" method="post" enctype="multipart/form-data">
	<div class="form-group">
		<label for="firstname">First name</label>
		<input type="text" class="form-control" name="user_firstname" value="<?php echo $db_firstname;?>"> </div>
	<div class="form-group">
		<label for="lastname">Last Name</label>
		<input type="text" class="form-control" name="user_lastname" value="<?php echo $db_lastname;?>"> </div>
	<div class="form-group">
		<label for="Username">Username</label>
		<input type="text" class="form-control" name="user_username" value="<?php echo $db_username;?>"> </div>
	<div class="form-group">
		<label for="form-group">Email</label>
		<input type="email" class="form-control" name="user_email" value="<?php echo $db_email;?>"> </div>
	<div class="form-group">
		<label for="form-group">Password</label>
		<input type="password" class="form-control" name="user_password"> </div>
	<div class="form-group">
		<label for="form-group">Repeat password</label>
		<input type="password" class="form-control" name="password_repeat"> </div>
	<div class="form-group">
		<input type="text" style="display:none" name="someinput"> </div>
	<div class="form-group">
		<input class="btn btn-primary" type="submit" name="updateprofile" value="Update details"> </div>
	<div>
		<?php echo updateProfile($link);?>
	</div>
</form>

Open in new window

0
Comment
Question by:Black Sulfur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Expert Comment

by:Mukesh Yadav
ID: 41802344
You can make variables global you want to use in form to echo their values and there is no need of while statement:
$row = $result->fetch_assoc();

Open in new window

will also work.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41802379
You might consider creating an object outside of the function and update it inside the function.  This is a step towards OOP.
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41802974
I want to try achieve it without using global as I believe it isn't great to use that.
0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 41803001
you don't, you return the variables from the function (i use an associative array) and use them like that.

So you'll see that instead of echo updateProfile, it's now echo $form_vars['message']
I can't see anywhere (unless i've missed it) you've included userid so you might want to add that in as a sure way of identifying the data coming back?

<?php include 'functions.php' ?>

<?php 
// $link should be available from your functions.php
$form_vars = updateProfile($link);
// $form_vars will contain an associative array (see new updateProfile function below)

?>
<form action="" method="post" enctype="multipart/form-data">
	<div class="form-group">
		<label for="firstname">First name</label>
		<input type="text" class="form-control" name="user_firstname" value="<?php echo $form_vars['firstname'];?>"> </div>
	<div class="form-group">
		<label for="lastname">Last Name</label>
		<input type="text" class="form-control" name="user_lastname" value="<?php echo $form_vars['lastname'];?>"> </div>
	<div class="form-group">
		<label for="Username">Username</label>
		<input type="text" class="form-control" name="user_username" value="<?php echo $form_vars['username'];?>"> </div>
	<div class="form-group">
		<label for="form-group">Email</label>
		<input type="email" class="form-control" name="user_email" value="<?php echo $form_vars['email'];?>"> </div>
	<div class="form-group">
		<label for="form-group">Password</label>
		<input type="password" class="form-control" name="user_password"> </div>
	<div class="form-group">
		<label for="form-group">Repeat password</label>
		<input type="password" class="form-control" name="password_repeat"> </div>
	<div class="form-group">
		<input type="text" style="display:none" name="someinput"> </div>
	<div class="form-group">
		<input class="btn btn-primary" type="submit" name="updateprofile" value="Update details"> </div>
	<div>
		<?php echo $form_vars['message'];?>
	</div>
</form>

Open in new window


updateProfile function
function updateProfile($link){
	
	
	
	$sql = "SELECT user_firstname, user_lastname, user_email FROM `users` WHERE user_username = '{$_SESSION['username']}'";
		$result = $link->query($sql);
			if($result->num_rows === 1){
				while($row = $result->fetch_assoc()){
					
					$db_firstname = htmlentities($row['user_firstname']);
					$db_lastname = htmlentities($row['user_lastname']);
					$db_username = htmlentities($_SESSION['username']);
					$db_email = htmlentities($row['user_email']);
					$db_userid = $row['user_id'];
				}
			}

	
	$message = "";
	
	if(isset($_POST['updateprofile'])){
		
		if(empty($_POST['user_firstname'])) {
			
			$message .= "First name required<br>";
		}
		
		if(empty($_POST['user_lastname'])) {
			
			$message .= "Last name is required<br>";
		}
		
		if(empty($_POST['user_username'])) {
			
			$message .= "Username is required<br>";
		}
		
		if(empty($_POST['user_email'])) {
			
			$message .= "Email address required";
		}
		
		if(!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {

			$message .= "Email address invalid";
		}
		
		if(!empty($_POST['someinput'])) {
				
				$message .= "Be gone spam bot!";
			}
		
		if ($message) {
			
			$message = "<div class='alert alert-danger'><strong>There were errors in your form:<br></strong>" .$message. "</div>";
			
		} else {
			
			$user_firstname = $link->real_escape_string($_POST['user_firstname']);
			$user_lastname = $link->real_escape_string($_POST['user_lastname']);
			$user_username = $link->real_escape_string($_POST['user_username']);
			$user_email = $link->real_escape_string($_POST['user_email']);
			
			
			$update_profile = "UPDATE `users` SET user_firstname = '$user_firstname', user_lastname = '$user_lastname', user_username = '$user_username', user_email = '$user_email' WHERE user_id = '$db_userid'";
				
			if($result = $link->query($update_profile) === TRUE){
				
				$message = "<div class='alert alert-success>User profile updated</div>";
			}
			
		}
	
	}
	
	return array(
		"message" => $message,
		"firstname" => $db_firstname,
		"lastname" => $db_lastname,
		"username" => $db_username,
		"email" => $db_email,
		"userid" => $db_userid
	);
		
	
	
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:Black Sulfur
ID: 41803031
That worked a treat, thank you! :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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