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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Keeping form values

    Hi, I have a form that has a select box of categories and a bunch of form fields.
    The categories has a link that says Add Categories, when you click on that it runs ajax to call the processAdd.php which adds the category to the database and then the ajax function does document.location.href = "page.php"

    what I want to know is there another way to do that and have the new item show up in the select box without deleting all of the other form input stuff that I already entered?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Yes. Instead of document.location.href replacement, the php would need to return (echo) the new option (as text or javascript) and the AJAX would need to add it in the select.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is there a way that i can make it return the mysql_insert_id and the $_POST['category'] and then how do i put it in the select statement
    maybe use appendChild?

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is what i have so far but it is still refreshing the page for some reason:

    Code:
    var xmlhttp = false;
    
    try {
    	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
    	try {
    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	} catch(E) {
    		xmlhttp = false;
    	}
    }
    if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    	xmlhttp = new XMLHttpRequest();
    }
    
    function submitCategory(cat) {
    	//make the box visible
    	serverPage = "processAdd.php?action=ajax&category="+cat;
    	obj = document.getElementById("types");
    	xmlhttp.open("GET", serverPage, true);
    	xmlhttp.onreadystatechange = function() {
    		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    		var stuff = xmlhttp.responseText.split(" ");
    		obj.appendChild("<option value=\""+stuff[0]+"\">"+stuff[1]+"</option>");
    		}
    	}
    	xmlhttp.send(null);
    	
    }
    and here is the php that im using in processAdd.php
    PHP Code:
    else if($action == "ajax") {
        
    $validator->validateGeneral($_REQUEST['category'], 'Category');
        if(
    $validator->foundErrors()) {
                
    header("Location: add.php?action=categories&msg=There was a problem with:<br /> ".$validator->listErrors('<br />'));
        } else {
        
            
    $cat trim(addslashes($_REQUEST['category']));
            
    $ch $db->query("SELECT title FROM categories WHERE name = '{$cat}'");
            
    $num $db->getNumRows($ch);
            if(
    $num 0) {
                
    header("Location: add.php?action=categories&msg=Sorry that category already exists in the database");
            } else {
                
    $result $db->query("INSERT INTO categories VALUES('', '{$cat}', 0)");
                echo 
    mysql_insert_id()." ".$cat;
            }
        }


  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone?

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    What action does the user take to run this?
    The refresh may be the result of a button click, link being followed, or a form submission. If it's a link, return false.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    //Javascript that directs to the ajax function
    function addCat() {
    	
    	var cat = prompt("Please type the name of the new category.");
    	if(cat) {
    		submitCategory(cat);
    	} else {
    		return false;
    	}
    	
    }
    //Link in HTML 
    <a href="#" onclick="return addCat();">Add Category</a>
    Oh and do you have any idea what this error means?

    [Exception... "Could not convert JavaScript argument" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://www.walrusmusicblog.com/_cms/js/xmlhttp.js :: anonymous :: line 24" data: no]
    http://www.walrusmusicblog.com/_cms/js/xmlhttp.js
    Line 24
    obj.appendChild("<option value=\""+stuff[0]+"\">"+stuff[1]+"</option>");
    Last edited by mhunt; 09-07-2007 at 05:00 PM.

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For anyone who wants to know the answer to this problem i just ended up doing this and it works perfectly.

    Code:
    unction submitCategory(cat) {
    
    	serverPage = "processAdd.php?action=ajax&category="+cat;
    	obj = document.getElementById("types");
    	xmlhttp.open("get", serverPage, true);
    	xmlhttp.onreadystatechange = function() {
    		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    		//var stuff = xmlhttp.responseText.split(" ");
    		//obj.appendChild("<option value=\""+stuff[0]+"\">"+stuff[1]+"</option>");
    		var xml = xmlhttp.responseXML.documentElement;
    		var id = xml.firstChild.childNodes[0].nodeValue;
    		var cat = xml.lastChild.childNodes[0].nodeValue;
    		var option_obj = new Option(cat, id);
    		obj.appendChild(option_obj);
    		}
    	}
    	xmlhttp.send(null);
    	
    }


  •  

    Posting Permissions

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