Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    form submission to reload div

    hey. i have a webpage with a menu so that when a link is clicked, a div on the same page loads the links content into it. This works fine. The content of the div is a web form, that when submitted, reloads the page to add the content to the database. the problem is that when the form is submitted, the whole page is re-loaded instead of the div. how can i change it so that when the form(in the div) is submitted, the page is only reloaded in the div?
    Code:
    <script type="text/javascript">
    var loadedobjects=""
    var rootdomain="http://"+window.location.hostname
    function ajaxpage(url, containerid){
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
    try {
    page_request = new ActiveXObject("Msxml2.XMLHTTP")
    } 
    catch (e){
    try{
    page_request = new ActiveXObject("Microsoft.XMLHTTP")
    }
    catch (e){}
    }
    }
    else
    return false
    page_request.onreadystatechange=function(){
    loadpage(page_request, containerid)
    }
    page_request.open('GET', url, true)
    page_request.send(null)
    }
    function loadpage(page_request, containerid){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    document.getElementById(containerid).innerHTML=page_request.responseText
    }
    function loadobjs(){
    if (!document.getElementById)
    return
    for (i=0; i<arguments.length; i++){
    var file=arguments[i]
    var fileref=""
    if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
    if (file.indexOf(".js")!=-1){ //If object is a js file
    fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", file);
    }
    else if (file.indexOf(".css")!=-1){ //If object is a css file
    fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", file);
    }
    }
    if (fileref!=""){
    document.getElementsByTagName("head").item(0).appendChild(fileref)
    loadedobjects+=file+" " //Remember this object as being already added to page
    }
    }
    }
    </script>
    </head>
    						<center>
    <div id="leftcolumn">
    <a href="javascript:ajaxpage('new_cat.php', 'rightcolumn');">New Category</a>
    <a href="javascript:ajaxpage('new_album.php', 'rightcolumn');">New Album</a>
    <a href="javascript:ajaxpage('new_user.php', 'rightcolumn');">New User</a>
    </div>
    <div id="rightcolumn"><h3>Choose a page to load.</h3></div>
    The form code:
    PHP Code:
    <?php
    include "../dbconnect.php";
    include 
    "../menu.php";
    if (isset (
    $_POST['cat_name'])){
    $cat_name mysql_real_escape_string($_POST['cat_name']);
    }
    if (isset (
    $_POST['cat_desc'])){
    $cat_desc mysql_real_escape_string($_POST['cat_desc']);
    }
    ?>
    <head>

    <script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
    </head>


                            <center>
                             <?php
                            
    if (isset($_POST['submit'])) { 
                            
    mysql_query ("INSERT INTO category (cat_name, cat_desc) VALUES ('$cat_name', '$cat_desc')");
                            
    ?>
                            <div id="txtHint">
                            <h1>Category added</h1><br /><br /><br /></div>
                            <a href='javascript:javascript:history.go(-2)'>BACK</a>
                            <?php
                            
    }
                            else
                            {
                            
    ?>
                            <div id="stylized" class="login">
                                    <h1>Create new Category</h1>
                                <p>Please enter details for the new Category</p>
                            <form id="new_category" method="post" action="new_cat.php">
                        
                                    <label>Category Name </label><input type="text" name="cat_name" size="50" />
                                    <label>Description</label><textarea rows="10" cols="40" name="cat_desc"></textarea>
                                    <button type="submit" name="submit" >Add New Category</button>
                            
                            <?php ?>
                            </center>

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,728
    Thanks
    6
    Thanked 1,015 Times in 988 Posts
    Since you are apparently already using jQuery (although a terribly outdated version) look into ajax() or get the form plugin.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,354
    Thanks
    23
    Thanked 618 Times in 617 Posts
    There is a lot missing in your code. As VIPStephan said, you call an old jquery library, but never use it. I got the first HTML file to call and load the second php file BUT the submission button called new_cat.php and where is that?

    Most important for you is:
    Code:
    if (isset ($_POST['cat_name'])){
    $cat_name = mysql_real_escape_string($_POST['cat_name']);
    }
    if (isset ($_POST['cat_desc'])){
    $cat_desc = mysql_real_escape_string($_POST['cat_desc']);
    }
    you have no safe guards for when the $_POST variables are not set.

  • #4
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I forgot to delete the link to the jquery code, its the remains of another page in my site. (but i've updated to a newer version now). The php in my previous post is "new_cat.php". Originally, it was its own page that checked if the variables had been set, then add them to the db, else display the form. But i want to display that page in a div, which is loaded from a menu on the side (like an iframe). The problem is when the form is submitted, the whole page is reloaded, not just the di as i want.


  •  

    Posting Permissions

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