Jump to content

Archived

This topic is now archived and is closed to further replies.

Maverickb7

Settings Help Please

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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:

[code]
<?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'>";
      .
      .
      .
  }
?>
[/code]
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.

Share this post


Link to post
Share on other sites
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.

[code]<?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");
?>[/code]

Share this post


Link to post
Share on other sites
in your submit tag in your form, you need to add [b]name = 'submit'[/b]

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 [b]else[/b] condition.  Leave the stuff inside it, of course. just remove [b]else {[/b] and it's closing bracket [b]}[/b] 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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.