Hey gang,

I have a small app that displays the contents of a MySQL database table, and am trying to get a form at the top to work...

Here is what I have - the app works fine, if the database info is input directly into the file,.. in an array like this:

"database"	 => array("host" => "localhost",
	 "username" => "user1",
         "password" => "mypassword",
         "database" => "test_db",
         "table" => "myTable"),
"sortable"			=> "all",
"editable"			=> "all",
.....
Table data is output here
.....

What I am trying to do is use a form, at the top of the same page that renders this data, to update the array items.. so, maybe the array would look something like this:

"database"	 => array("host" => $_POST['host'],
	 "username" => $_POST['user_name'],
	 "password" => $_POST['password'],
	 "database" => $_POST['database'],
	 "table" => $_POST['table']),
"sortable"			=> "all",
"editable"			=> "all",
.......
table data is output here
.......

and my form at the top of the page looks like this:

	<form method="POST" action="index2.php">
	<label>Host:</label>
	<input type="text" name="host" id="host" value="" size="30" maxlength="10" />
	<label>Username:</label>
	<input type="text" name="user_name" id="user_name" value="" size="30" maxlength="10" />
	<label>Password:</label>
	<input type="text" name="password" id="password" value="" size="30" maxlength="10" />
	<label>DB Name:</label>
	<input type="text" name="database" id="database" value="" size="30" maxlength="10" />
	<label>Table:</label>
	<input type="text" name="table" id="table" value="" size="30" maxlength="10" />
	<input type="submit" value="submit" />
	</form>

I'm no JS wiz, but I presume that AJAX might be a way to go when updating variables without leaving the page or sending the data to another page...
Any help is greatly appreciated !!

Thanks again!


    UPDATE


    the 'action' in the form set as 'index2.php' was a tem test.. and has been removed...

      if you post something from a form to a .php file you need to save them into a file.

      once you post variables, you get it in the $_POST superglobal

      lets create a string on the landing page to build the array with the parameters.

      <?php
      if(!empty($_POST["param1"]) AND !empty($_POST["param2"]))
      {
      // if you're using apostrofes you need to escape the apostrofes
      $out='<?php
      
      $table=\'Something!\';
      $array=  array("param1"=>"'.$_POST["param1"].'"
      ,"param2"=>"'.$_POST["param2"].'");
      
      ?>';   //end of the string
      
      //print or save into a new file:
      
      file_put_contents("filename.php" , $out);
      
      }
      ?>

      is this what you wanted to do ?

        To be honest.. I'm not sure it that is what I am needing.. notice the 'not sure' part.. lol.

        I want to use a form at the top of page1 (so to speak) to popular variables in the same page, for use in a php script..
        will this do that?

        djjjozsi;10920528 wrote:

        if you post something from a form to a .php file you need to save them into a file.

        once you post variables, you get it in the $_POST superglobal

        lets create a string on the landing page to build the array with the parameters.

        <?php
        if(!empty($_POST["param1"]) AND !empty($_POST["param2"]))
        {
        // if you're using apostrofes you need to escape the apostrofes
        $out='<?php
        
        $table=\'Something!\';
        $array=  array("param1"=>"'.$_POST["param1"].'"
        ,"param2"=>"'.$_POST["param2"].'");
        
        ?>';   //end of the string
        
        //print or save into a new file:
        
        file_put_contents("filename.php" , $out);
        
        }
        ?>

        is this what you wanted to do ?

          So,.. here is my form and the variables I need insert from the POST data.. into the fields below...

          	<form name="form" method="post" action="">
          	<label>Host:</label>
          	<input type="text" name="host" id="host" value="" size="30" maxlength="10" />
          	<label>Username:</label>
          	<input type="text" name="user_name" id="user_name" value="" size="30" maxlength="10" />
          	<label>Password:</label>
          	<input type="text" name="password" id="password" value="" size="30" maxlength="10" />
          	<label>DB Name:</label>
          	<input type="text" name="database" id="database" value="" size="30" maxlength="10" />
          	<label>Table:</label>
          	<input type="text" name="table" id="table" value="" size="30" maxlength="10" />
          	<input type="submit" value="submit" />
          	</form>
          	<?
          
          include("TableGear.php");
          
          $table = new TableGear(array(
          
          //  This first line is your database configuration.
          //  You MUST include this for TableGear to work!
          //  Replace all braces <> with your database info.
          
          "database"	 => array("host" => $_POST['host'],
          	 "username" => $_POST['user_name'],
          	 "password" => $_POST['password'],
          	 "database" => $_POST['database'],
          	 "table" => $_POST['table']),
          "sortable"			=> "all",
          "editable"			=> "all",

          So, by staying on the same page, I can input the variables and have the page refresh with the new data..

            just a few basics 🙂

            use a variable if it has set. before you use a variable
            check if that has a value.

            posted values only exists after you submit the form.

            my code creates a string based on the posted values, and makes a new file while these values will embedded into the php code.

            what is your goal with the filled values?
            there would be better ways to store table details: database or sessions.

            jjozsi

              great.. thank you for the info. (much needed!)

              What I am trying to do is use a form at the top of a page, this page contains a JS that pulls data from a DB and allows you to view/edit it on screen.. so, instead of having to change the config file each time.. it would be great to be able to place a form at the top of this page.. then just input the DB name, user, etc. etc. and have the form change the varibles in the script, instead of the script looking for the config file.. OR.. I guess the form could also update the config file and then when the page is refreshed it would see the udpated data in the config file and display that.. make sense??

                make sense, and its an interesting project 🙂

                first, ask for the mysql settings for the connection.

                save them into the connDB.php file.

                its a better way to manage table details if you store it in a database/table:
                call it 4 example table_definitions
                you can add the field names and its properties, settings for validation into this table.

                if you write me how many fields needed to edit online, i can send you two simple xample code for insert/select and modify via AJAX🙂

                  Ok.. I gathered some more info on 'where' the co. got this app.. it is from here:
                  http://www.andrewplummer.com/code/tablegear/

                  and I'm not aware of any other mods to it, other than this one I am trying to piece together for my use (within the co.).. let me know what you think..
                  😃

                    Write a Reply...