Jump to content

Multiple Forms/Info in 1 page, echo?


jad3z

Recommended Posts

Hello guys!

 

Thanks for reading my topic!

I am kinda new to PHP and i try to get all these forms in 1 page

Can you guys help me with that?
 
 
start.php

<?php
$file = 'user01.ini';
    
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    $data = '';
    foreach($_POST AS $name=>$value) {
        $data .= $name . '=' . $value .PHP_EOL;
    }
 
    file_put_contents($file, $data, LOCK_EX);
    if(file_exists($file)) {
        echo 'Saved in <a href="' . $file . '">' . $file . '</a>';
    } else {
        echo 'Failed';
    }
}
 
if(file_exists($file)) {
    $ini = parse_ini_file($file);
}
?>
<form method="POST">
 
<label>IP</label>
<input type="numbers" name="ip" value="<?php echo (isset($ini['ip']) ? $ini['ip'] : '127.0.0.1'); ?>" /><br />
 
<label>Users</label>
<input type="number" name="users" value="<?php echo (isset($ini['users']) ? $ini['users'] : '10'); ?>" /><br />
 
<label>Website</label>
<input type="text" name="website" value="<?php echo (isset($ini['website']) ? $ini['website'] : 'http://'); ?>" /><br />
 
<button>Start</button>
</form>

 
add.php

<?php
 $file = fopen("user01.ini","r");
  if(!file)
    {
      echo("ERROR:cant open file");
    }
    else
    {
      $buff = fread ($file,filesize("user01.ini"));
      print $buff;
    }
 
$file = 'user01.ini';
    
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    $data = '';
    foreach($_POST AS $name=>$value) {
        $data .= $name . '=' . $value .PHP_EOL;
    }
 
    file_put_contents($file, $data, LOCK_EX);
    if(file_exists($file)) {
        echo 'Saved in <a href="' . $file . '">' . $file . '</a>';
    } else {
        echo 'Failed';
    }
}
 
if(file_exists($file)) {
    $ini = parse_ini_file($file);
}
?>
<form method="POST">
 
<label>Add Users</label>
<input type="number" name="addusers" value="<?php echo (isset($ini['addusers']) ? $ini['addusers'] : '10'); ?>" /><br />
 
<button>Add</button>
<button>Stop</button>
</form>

 
stop.php

<?php
$file = 'user01.ini';
    
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    $data = '';
    foreach($_POST AS $name=>$value) {
        $data .= $name . '=' . $value .PHP_EOL;
    }
 
    file_put_contents($file, $data, LOCK_EX);
    if(file_exists($file)) {
        echo 'Saved in <a href="' . $file . '">' . $file . '</a>';
    } else {
        echo 'Failed';
    }
}
 
if(file_exists($file)) {
    $ini = parse_ini_file($file);
}
?>
<form method="POST">
 
<label>Server has stopped</label><br>
 
<label>IP</label>
<input type="numbers" name="ip" value="<?php echo (isset($ini['ip']) ? $ini['ip'] : '127.0.0.1'); ?>" /><br />
 
<label>Users/label>
<input type="number" name="users" value="<?php echo (isset($ini['users']) ? $ini['users'] : '10'); ?>" /><br />
 
<label>Website</label>
<input type="text" name="website" value="<?php echo (isset($ini['website']) ? $ini['website'] : 'http://'); ?>" /><br />
 
<button>Start</button>
</form>

 
Is it possible to code this on one page? Maby with echo function ?
 
start.php 
start > add.php (index.php?p=add)
 
add.php 
add > add.php (index.php?p=add)
stop > stop.php (index.php?p=stop)
 
stop.php
start > start.php (index.php?p=start)
 
Thanks in advanced guys! Im new to php and i think this forum is the place to be!
 

Link to comment
Share on other sites

you need a simple 'page controller' that will determine what action the code on the page takes and what content is displayed on the page when it receives a request. you can use a switch/case statement with an include statement in the different case conditions to include the appropriate code you need for each case.

Link to comment
Share on other sites

you need a simple 'page controller' that will determine what action the code on the page takes and what content is displayed on the page when it receives a request. you can use a switch/case statement with an include statement in the different case conditions to include the appropriate code you need for each case.

Thanks for the response Gyer!

 

Do you got any examples?

 

Or tutorial/links ?

 

THanks in advanced

Link to comment
Share on other sites

So, you could use a central php script that plays the role of a controller.

Then, based on the action, this central PHP script will choose which PHP (that you already have) to include.

<?php

switch ($action) {
    case "add":
        include 'add.php';
        break;
    case "start":
        include 'start.php';
        break;
    case "stop":
        include 'stop.php';
        break;
}

?>

So you need your form to always call this script and pass a parameter called "action" depending on the button clicked.

 

Does it make sense?

Link to comment
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.