Jump to content


Photo

Settings Help Please


  • Please log in to reply
6 replies to this topic

#1 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 10 September 2006 - 07:17 AM

Hello, I'm creating a CMS and I'm trying to refresh my memory as I haven't coded php for a while. Anyways, I've created a settings page that holds form that pulls settings from my database and allows me to edit them. Now when I click submit i want it to update the database with the updated information. I know how to do this using two pages, but how would i go about doing this within the same page?

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 September 2006 - 07:28 AM

in your form action instead of doing action ='blah.php' use $_SERVER['PHP_SELF'] and it will reload the same page. Then just setup your conditions the same as you would have had on the 2nd page, except just put them together. example:

<?php
   if (isset($_POST['submit'])) {
      // do form checking and db updating here
   } else {
     // get info from db to show settings 

     // echo form
      echo "<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>";
      .
      .
      .
   }
?>
or you could skip enclosing the form and stuff inside the else { .. } to have the form be redisplayed automatically, whenever you click submit. matter of preference, really.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 10 September 2006 - 03:18 PM

I tried putting this into play but it doesn't seem to work. The code I'm using is below. I have one other file included that is not shown.. it has the database information.

<?php
	include("/home/templates/global_header.php");
$site_name = $_POST['site_name'];
$site_url = $_POST['site_url'];
if (isset($_POST['submit'])) {


$connect = mysql_connect($DBhost, $DBuser, $DBpass);
mysql_select_db($DBname,$connect) or die ("Could not select database");
mysql_query($connect);

$q = "SELECT * FROM settings";
$r = mysql_query($q);

$row = mysql_fetch_array($r);
$site_name = $row['site_name'];
$site_url = $row['site_url'];
$query = "UPDATE site_settings SET site_url = '$site_url', site_name = '$site_name'";
mysql_query($query);
	echo "<center><h2>Thank you,<br>Your settings have been updated.</h2></center>";
	echo "<meta http-equiv=Refresh content=2;url=settings.php>";
}

else {
$connect = mysql_connect($DBhost, $DBuser, $DBpass);
mysql_select_db($DBname,$connect) or die ("Could not select database");
mysql_query($connect);

$q = "SELECT * FROM site_settings";
$r = mysql_query($q);

$row = mysql_fetch_array($r);
$site_name = $row['site_name'];
$site_url = $row['site_url'];
echo "<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>";
?>
<span class="largetext">Modify Settings</span><br>
<span class="smalltext">Customize the settings below to your liking.</span><br>
<br>
<table cellspacing="0" cellpadding="2" width="100%" align="center">
<tr><td><span class="smalltext"><b>Site Name:</b></span></td><td><input class="textfield" type="text" size="52" name="site_name" value="<? echo $site_name; ?>"></td></tr>
<tr><td><span class="smalltext"><b>Site URL:</b></span></td><td><input class="textfield" type="text" size="52" name="site_url" value="<? echo $site_url; ?>"></td></tr>
<tr><td colspan="2" align="center"><input type='hidden' name='zv_settings' value='Website_Settings' /><input class="submit" type="Submit" value="submit"></td></tr>
</table></form>

<?php
}
	include("/home/templates/global_footer.php");
?>


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 September 2006 - 06:21 PM

in your submit tag in your form, you need to add name = 'submit'

other than that, the code looks fine to me, other than the fact that since you are connecting to the database in both your conditions, you should take it out of the conditions and put it before the conditions, instead of having the same code in each condition.

also, if this script here is settings.php, then instead of having a refresh tag after the update, remove the else condition.  Leave the stuff inside it, of course. just remove else { and it's closing bracket } That way, whenever you access the script, even when you update the database, the last thing it will do is select the info and display the form.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 10 September 2006 - 07:41 PM

Still doesn't seem to work for me. After I hit fresh it just reloads the main and no changes were made. :S

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 September 2006 - 07:45 PM

okay post what you got since lots of stuff has been changed. let's see what it looks like now.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 10 September 2006 - 08:03 PM

Alright I did a little messing around and I seemed to get things working after using the switch function. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users