refresh page after changing nick name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mike667
Forum Newbie
Posts: 2
Joined: Wed Dec 15, 2010 9:14 am

refresh page after changing nick name

Post by mike667 »

hi all, i've only just began learning php and im a bit stuck on this. i have a page where the user can change his name on his profile, the page should refresh and display his new name in the text box but it isnt doing it, however it is changing the name in the databse but is only showed the change next time i log in.

Code: Select all

	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<div class="content">
		<table class="tform">
		<col class="col_first" />
		<col class="col_second" />
		<tbody>
			<tr>
				<td><label>Screen name:</label></td>
				<td><input type="text" class="text" name="nick_name" id="profile_name" value=" <?php echo $_SESSION['SESS_NICK_NAME'];?> "  /></td>

			</tr>
									<tr>
				<td></td>
				<td class="tleft">
					<button class="blue" type="submit" name="change_name">Save Changes</button>
					&nbsp;&nbsp;
									</td>
			</tr>

		</tbody>
		</table>
	</div>
	</form>
</div>

<?php 

if(isset($_POST['change_name'])) 
{ 
    $name = $_POST['nick_name'];
	
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
		$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
	
	$qry = "UPDATE `tmp`.`members` SET `firstname` = '" . $name . "' WHERE `members`.`member_id` =" . $_SESSION['SESS_MEMBER_ID'] . ";";
	$result = @mysql_query($qry);
	
	//Check whether the query was successful or not
	if(!$result) {
		die("Query failed");
	}
	
	$msg = "You have changed you're name to " . $name; 
	echo "<script langauge=\"javascript\">alert(\"".$msg."\");</script>"; 
	echo "<META HTTP-EQUIV=refresh CONTENT=15>";
}

?>

User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: refresh page after changing nick name

Post by social_experiment »

You will have to set the session variable $_SESSION['SESS_NICK_NAME'] again after the nick has been updated.

Code: Select all

<?php $_SESSION['SESS_NICK_NAME'] = $newNick;  ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mike667
Forum Newbie
Posts: 2
Joined: Wed Dec 15, 2010 9:14 am

Re: refresh page after changing nick name

Post by mike667 »

hmm not sure that would work, the user logs in to his profile and can see his name in a text box, he changes it and clicks "save" his new name is now shown in that text box. so i tried this:

Code: Select all

if(isset($_POST['change_name'])) 
{ 

    $name = $_POST['nick_name'];
	$qry = "UPDATE `tmp`.`members` SET `firstname` = '" . $name . "' WHERE `members`.`member_id` =" . $_SESSION['SESS_MEMBER_ID'] . ";";
	$result = @mysql_query($qry);
	if(!$result) die("Query failed");
	
	echo "<script langauge=\"javascript\">location.reload(true);</script>";
	
}

function my_name() {
	$name="";
	$qry="SELECT * FROM members WHERE member_id='".$_SESSION['SESS_MEMBER_ID']."'";
	$result=mysql_query($qry);
	if($result) {
		if(mysql_num_rows($result) == 1) {
	
			$member = mysql_fetch_assoc($result);
			$name = $member['firstname'];
		}
	}else{
		die("Query failed");
	}
	return $name;
}
it works, apart from the page keeps refreshing all the time instead of just once.. would be nice if there was a way to do it without the page refreshing after clicking "save" :/ or atleast so it refreshes only once.

im getting annoyed having to do this capture thing whenever i post - i cant read the letters most the time lol
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: refresh page after changing nick name

Post by social_experiment »

Good for you, necessity is the mother of all invention.
mike667 wrote:it works, apart from the page keeps refreshing all the time instead of just once.. would be nice if there was a way to do it without the page refreshing after clicking "save" :/ or atleast so it refreshes only once.
Google AJAX
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply