Adding a Submit box to a Php Script? Need help

Discussion in 'Web Development & Web Hosting' started by CliffG, Dec 6, 2005.

  1. CliffG

    CliffG Nibble Poster

    72
    3
    17
    Hi there,

    I've got a php script that performs a backup of a mysql db. At the moment, just by visiting the url of the php script will run the backup.

    What i'm after is that when you visit the page, it will have a small dialogue and a submit button that will then run the script, but i don't know how to add it.. Any help would be appreciated..

    Here is the script...

    PHP:
    <?php
      
    // Enter your MySQL access data 
      
    $host'localhost'// hostname, usually localhost         
      
    $user'database username';  // database username             
      
    $pass'password'// database password
      
    $db=   'database name'//name of the database

      
    $backupdir 'directory';  //directory to copy the database 

      // Compute day, month, year, hour and min.
      
    $today getdate();
      
    $day $today[mday];
      if (
    $day 10) {
          
    $day "0$day";
      }
      
    $month $today[mon];
      if (
    $month 10) {
          
    $month "0$month";
      }
      
    $year $today[year];
      
    $hour $today[hours];
      
    $min $today[minutes];
      
    $sec "00";


      
    // Execute mysqldump command.
      // It will produce a file named $db-$year$month$day-$hour$min.sql
      // under $DOCUMENT_ROOT/$backupdir
      
    system(sprintf(
        
    'mysqldump --opt -h %s -u %s -p%s %s > %s/%s/%s-%s%s%s-%s%s.sql',                                                 
        
    $host,
        
    $user,
        
    $pass,
        
    $db,
        
    getenv('DOCUMENT_ROOT'),
        
    $backupdir,
        
    $db,
        
    $year,
        
    $month,
        
    $day,
        
    $hour,
        
    $min
      
    )); 
      echo 
    'DONE. The database is backed up'
    ?>
    Thank you
     
    Certifications: A+
    WIP: Network+
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    You need to exit php with:
    ?>
    then write some html like:
    <html>
    <title>My cool backup</title>
    <body>
    <form method=post action="anotherpage.php">
    some info here
    some input <input type=text name=ineedit>
    <p>
    <input type=submit value="button text">
    </form>
    </body>
    </html>

    You will then need to start another page by retrieving the value from "ineedit".

    The above was written from memory - any decent book on HTML will give you more detail (and probably more accurate) - look for 'forms'.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  3. CliffG

    CliffG Nibble Poster

    72
    3
    17
    Thanks hbroomhall,

    To be honest, I didn't really want to do it in 2 files. However since this thread started i've managed to get a script together that does work..

    For reference, this is it..

    PHP:
    <?php

    //this means that if the submit button has been pressed, do all of this
    if(isset($_POST['submit']))
    {
      
    // Enter your MySQL access data 
      
    $host'localhost'// hostname, usually localhost         
      
    $user'database username';  // database username             
      
    $pass'password'// database password
      
    $db=   'database name'//name of the database

      
    $backupdir 'directory';  //directory to copy the database 

      // Compute day, month, year, hour and min.
      
    $today getdate();
      
    $day $today[mday];
      if (
    $day 10) {
          
    $day "0$day";
      }
      
    $month $today[mon];
      if (
    $month 10) {
          
    $month "0$month";
      }
      
    $year $today[year];
      
    $hour $today[hours];
      
    $min $today[minutes];
      
    $sec "00";


      
    // Execute mysqldump command.
      // It will produce a file named $db-$year$month$day-$hour$min.sql
      // under $DOCUMENT_ROOT/$backupdir
      
    system(sprintf(
        
    'mysqldump --opt -h %s -u %s -p%s %s > %s/%s/%s-%s%s%s-%s%s.sql',                                                 
        
    $host,
        
    $user,
        
    $pass,
        
    $db,
        
    getenv('DOCUMENT_ROOT'),
        
    $backupdir,
        
    $db,
        
    $year,
        
    $month,
        
    $day,
        
    $hour,
        
    $min
      
    )); 
      echo 
    'DONE. The database is backed up';
    }

    //this means that if the submit button hasn't been pressed, print the form!
    else
    {
        print 
    '
    <form action="'
    .$_SERVER['PHP_SELF'].'" method="POST">
    <input type="submit" name="submit" value="Back up DB"></input>
    </form>
    '
    ;
    }
    ?> 
     
    Certifications: A+
    WIP: Network+
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Ah! Glad you got it working.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.