Jump to content

Recommended Posts

I'm trying to make a admin feature that allows my to turn on/off maintenance. When it is on it will redirect users to a "Sorry site is down" page. I want it to consist of a page with the  form for turning it on/off, the page with my function, and calling the function on my sites pages.

 

this is the function

function maintenance_on_off($maint_on_off) {
if ($maint_on_off == On) {
	header ('Location: ../maintenance.php');

} 
}

The problems I'm having are..

 

1. When I call the function I get the error:

 

Warning: Missing argument 1 for maintenance_on_off(), called in /home/netgame2/public_html/index.php on line 3 and defined in /home/netgame2/public_html/includes/maintenance_on_off.php on line 3

 

2. I'm not sure how to create a form to write to the page with the function.

 

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/
Share on other sites

Keep in mind I'm just starting PHP.

 

I call it like:

<?php
include_once("includes/maintenance_on_off.php");
maintenance_on_off();
?>

 

Now it I Call it like:

<?php
include_once("includes/maintenance_on_off.php");
maintenance_on_off(On);
?>

 

it works. What I want is to turn it on off from the admin form. Instead of coding it.

 

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/#findComment-1026369
Share on other sites

Keep in mind I'm just starting PHP.

 

I call it like:

<?php
maintenance_on_off();
?>

 

Now it I Call it like:

<?php
maintenance_on_off(On);
?>

 

it works. What I want is to turn it on off from the admin form. Instead of coding it.

 

 

Okay.  You can't call your function without passing it an argument (a value within the ()'s).  Why?  Because you defined it to always take one argument ($maint_on_off).  So, you need to call it like so:

 

maintenance_on_off("On");

//or

maintenance_on_off("Off");

 

Also, remember that string values (e.g., text) must be encased in quotes, so you should always use quotes when invoking the function, and you should change your function body to:

 

function maintenance_on_off($maint_on_off)
{
   if ($maint_on_off == "On") { header("Location: ../maintenance.php"); }
}

 

In this case, it doesn't really matter if you use single or double quotes, so long as you actually use quotes of some sort.

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/#findComment-1026375
Share on other sites

OK I get that.

 

Now, is there a way to do what I want?

 

 

function maintenance_on_off() {
$maint_on_off = "On";
if ($maint_on_off == "On") {
	header ('Location: ../maintenance.php');

} 
}

 

This works. But I still want to change the variable $maint_on_off value from a form instead of hard coding it.

 

I know how to post to a database, but I don't get how to post to a variable on a different page.

 

this is what I'm trying to work with.

<?php

echo "
<form method=\"POST\" action=\"$_POST["???????"]\" >
  <p>On:<br/>
    <input type=\"checkbox\" name=\"on\" value=\"On\">
    <p>Off:<br/>
    <input type=\"checkbox\" name=\"off\" value=\"Off\">
  <p>
      <input type=\"submit\" name=\"submit\"  value=\"Submit\">     
</form>";

?>

 

I have a feeling it has something to do with $fopen, but I don't know how to apply it to a from.

 

 

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/#findComment-1026397
Share on other sites

Why not store and update the value in a db?

 

<?php
   function maintenance_on_off($maint_on_off)
   {
      $query = "UPDATE table SET maintenance = $maint_on_off";
      $result = mysql_query($query);

      if ($maint_on_off == "On") { header("Location: ../maintenance.php"); }
      else if ($maint_on_off == "Off") { /* do non-maintenance stuff */ }
      else { /* error */ }
   }

   if (isset($_POST['submit'])) { maintenance_on_off($_POST['maintStatus']); }
?>

<!DOCTYPE html>
<html>
   <head></head>

   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
         On: <input type="checkbox" name="maintStatus[]" value="On" />
         Off: <input type="checkbox" name="maintStatus[]" value="Off" />
         <br />
         <input type="submit" name="submit" value="Submit" />
      </form>
   </body>

</html>

 

If you want to post to a different page, instead of having the form post to itself like I have it do above, simply put the path to the form handling code file as the form's action.  Within that file, simply access the data through the $_POST superglobal array like always.  So, in that file, you'd still access the checkbox data with $_POST['maintStatus'], and so on.

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/#findComment-1026409
Share on other sites

Yeah, I was trying to avoid creating a database just for that, but it is probably the best way. I'll give it a shot.

 

 

Thanks.

 

Well, I don't think there's much difference between using a db versus a text file in this case, aside from the reading/writing/updating mechanics.  I'm a bit more comfortable using db's than writing to/reading from files simply because I interact with db's far more often in my daily activities.

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/#findComment-1026432
Share on other sites

I've been trying to get the code Nightslyr posted to work, but I'm getting an the error

 

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/netgame2/public_html/admin/maint_on_off.php on line 14

 

<?php
$mysqli = mysqli_connect("localhost", "user name", "password", "database name");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
mysqli_close($mysqli);
}

   function maintenance_on_off($maint_on_off)
   {
      $query = "UPDATE maintenance SET on_off = $maint_on_off";
      $result = mysqli_query($mysqli, $query); //Line 14

      if ($maint_on_off == "On") { header("Location: ../maintenance.php"); }
      else if ($maint_on_off == "Off") { /* do non-maintenance stuff */ }
      else { /* error */ }
   }

   if (isset($_POST['submit'])) { maintenance_on_off($_POST['maintStatus']); }
?>

<!DOCTYPE html>
<html>
   <head></head>

   <body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
         On: <input type="checkbox" name="maintStatus[]" value="On" />
         Off: <input type="checkbox" name="maintStatus[]" value="Off" />
         <br />
         <input type="submit" name="submit" value="Submit" />
      </form>
   </body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/195317-form-help/#findComment-1026671
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.