dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: create a new page on submit from database

  1. #1
    Join Date
    Mar 2014
    Posts
    3

    create a new page on submit from database

    Hi everybody,

    I'm starting with PHP and Mysql, so it's getting diffictul at this poinf for me and i'm looking for a bit of help =).

    I've created a chained menu using php and a database, following this tutorial:

    http://www.yourinspi...php-and-jquery/

    The frist table content a list of categories like :

    CREATE TABLE IF NOT EXISTS `chainmenu_categories` (
    `id_cat` int(4) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(40) NOT NULL,
    PRIMARY KEY (`id_cat`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;

    My second table, the type, like :


    CREATE TABLE IF NOT EXISTS `type` (
    `id_type` int(4) unsigned NOT NULL AUTO_INCREMENT,
    `id_cat` int(4) unsigned NOT NULL,
    `name` varchar(40) NOT NULL,
    `destination` varchar(40) NOT NULL,
    PRIMARY KEY (`id_type`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

    I managed, once cliking on submit, to redirect to an other page by that in my select.php:

    PHP Code:
    var result = $("select#type option:selected").html();
    $(
    "#select_form").submit(function( event ) {
      var 
    the_url = $("#type").val();
      
    window.location the_url;
      
    event.preventDefault();
    }); 
    and adding this on my select.class.php
    PHP Code:
    public function ShowCategory()
            {
                
    $sql "SELECT * FROM chainmenu_categories";
                
    $res mysql_query($sql,$this->conn);
                
    $category '<option value="0">choose...</option>';
                while(
    $row mysql_fetch_array($res))
                {
                    
    $category .= '<option value="' $row['id_cat'] . $row['destination']. '">' $row['name'] . '</option>';
                }
                return 
    $category;
            } 
    So now it redirect each time to a different page depending of the option choose from the menu, like: http://mydomain.com//3 or http://mydomain.com//4 or http://mydomain.com//5 etc etc

    But as the page doesnt exist, it redirect each time to a dead link,

    Is anybody can give me a help to create these pages from the chained menu ( or have some highlite) ? and If possiblles, some pointer to create the admin interface to allow an admin to add the pages / chained menu ?

    I've been trying to start with something which look like:

    PHP Code:
    <?php require('db_config.php'); 
     
     
     
    $stmt $db->prepare('SELECT id_type, name FROM type WHERE id_cat=$_POST[id]');
    $stmt->execute(array(':id_cat' => $_GET['name']));
    $row $stmt->fetch();
    but dont know if i'm in the good way at all, I' just so confused !

    Thank you in advance for your recommandations guys !!

  2. #2
    Join Date
    Mar 2014
    Posts
    3
    the link of the original tutorial ( without the submit working) is:

    http://www.yourinspirationweb.com/en...hp-and-jquery/

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,091
    your code doesn't look too bad. Try turning on error checking to see what errors you are probably getting.

    BTW - using "type" for a table name could be the problem.

  4. #4
    Join Date
    Mar 2014
    Posts
    3
    Hi,

    I'm really struggling since the last 48 hours with that,

    for the moment, nothing is working, is there possiblity to get a bit of hihglit ??

    Here is my full code, may be better this way . . .

    The select.class.php

    PHP Code:
    <?php
    class SelectList
    {
        protected 
    $conn;
     
            public function 
    __construct()
            {
                
    $this->DbConnect();
            }
     
            protected function 
    DbConnect()
            {
                include 
    "db_config.php";
                
    $this->conn mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
                
    mysql_select_db($db,$this->conn) OR die("can not select the database $db");
                return 
    TRUE;
            }
     
            public function 
    ShowCategory()
            {
                
    $sql "SELECT * FROM chainmenu_categories";
                
    $res mysql_query($sql,$this->conn);
                
    $category '<option value="0">choose...</option>';
                while(
    $row mysql_fetch_array($res))
                {
                    
    $category .= '<option value="' $row['id_cat'] . $row['destination']. '">' $row['name'] . '</option>';
                }
                
                if(
    file_exists($name.'.php'))
        echo 
    "file : " $name.'.php' " is exist";
    else
    {
      
    $file fopen($name.'.php',"w");
      
    $code="here what U want to write inside the new page.php";
      
    fwrite($file,$code);
      
    fclose($file);
    }
                return 
    $category;
                
            }
            
            
     
            public function 
    ShowType()
            {
                
    $sql "SELECT * FROM type WHERE id_cat=$_POST[id]";
                
    $res mysql_query($sql,$this->conn);
                
    $type '<option value="0">choose...</option>';
                while(
    $row mysql_fetch_array($res))
                {
                    
    $type .= '<option value="' $row['id_type'] . '">' $row['name'] . '</option>';
                }
                return 
    $type;
                
                
            }
    }


     
    $opt = new SelectList();
    ?>
    and the select.php
    HTML Code:
    <!DOCTYPE html>
    
        <head>
        
        <meta charset="UTF-8" />
    
    	<script type="text/javascript" src="jquery-1.3.2.js"></script>
        	<script type="text/javascript">
    		$(document).ready(function(){
    			$("select#type").attr("disabled","disabled");
    		
    			$("select#category").change(function(){
    			$("select#type").attr("disabled","disabled");
    			$("select#type").html("<option>wait...</option>");
    
    			var id = $("select#category option:selected").attr('value');
                                                           
    			$.post("select_type.php", {id:id}, function(data){
    				$("select#type").removeAttr("disabled");
           			$("select#type").html(data);
                  });
          		});
    
             	$("form#select_form").submit(function(){
    				var cat = $("select#category option:selected").attr('value');    
    				var type = $("select#type option:selected").attr('value');
    				if(cat>0 && type>0)
    				{
    					var result = $("select#type option:selected").html();
    $("#select_form").submit(function( event ) {
      var the_url = $("#type").val();
      window.location = the_url;
      event.preventDefault() ;
      
    });
    
    				}
    				else
    				{
    					$("#result").html("You must choose two options!");
    				}
    
    				return false;                                                         
    
                });
    			
    			
    		});
    
        </script>
        </head>
        <body>
            <?php include "select.class.php"; ?>
            <form id="select_form">
                Choose a category:<br />
                <select id="category">
                    <?php echo $opt->ShowCategory(); ?>
                </select>
            <br /><br />
            Choose a region:<br />
            <select id="type">
                 <option value="0">choose a domain</option>
            </select>
            <br /><br />
          <input type="submit" value="Go" />
            </form>
            <div id="result"></div>
        </body>
    </html>
    I don't know where to go, what to do . . . . please help me =)
    Last edited by jedaisoul; 03-22-2015 at 12:53 PM.

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,091
    Did you turn on error checking yet?

  6. #6
    Join Date
    Mar 2015
    Posts
    1

    Very Nice Information

    Its everything here exactly what I was looking for Im so happy that I step in right here.

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,937
    I would advise that your using mysql_* functions, you should be using mysqli_* functions as the mysql_* functions are now depreciated and it only takes an update by your web host and your script ceases to work... Better off putting the work in place now to save yourself the bother later.
    STOP using $ prefix on JavaScript variable names...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles