Jump to content


Photo

Multiple Forms/Info in 1 page, echo?


  • Please log in to reply
5 replies to this topic

#1 jad3z

jad3z

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 02 March 2014 - 10:44 AM

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!
 



#2 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,437 posts
  • LocationVoorheesville NY

Posted 02 March 2014 - 01:15 PM

Read thru your code but don't know what you are trying to do.  Can you at least describe your goal.  And - please define what you mean by page and form, just to be sure we agree.


JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#3 jad3z

jad3z

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 02 March 2014 - 04:45 PM

Gr2dvFz.jpg



#4 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,364 posts

Posted 02 March 2014 - 05:48 PM

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.


multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#5 jad3z

jad3z

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 02 March 2014 - 05:50 PM

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



#6 mogosselin

mogosselin

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationMontreal

Posted 02 March 2014 - 10:28 PM

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?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com