Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-16-2012, 10:53 AM   PM User | #1
woodplease
New Coder

 
Join Date: Nov 2009
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
woodplease is an unknown quantity at this point
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>
woodplease is offline   Reply With Quote
Old 07-16-2012, 11:49 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,705
Thanks: 5
Thanked 876 Times in 851 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Since you are apparently already using jQuery (although a terribly outdated version) look into ajax() or get the form plugin.
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Old 07-16-2012, 03:23 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 362 Times in 361 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Old 07-16-2012, 03:36 PM   PM User | #4
woodplease
New Coder

 
Join Date: Nov 2009
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
woodplease is an unknown quantity at this point
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.
woodplease is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:52 AM.


Advertisement
Log in to turn off these ads.