Have a problem with a javascript form submit.

I have a script that when you start typing a name looks up that name in database.
If the name is found  a hidden div becomes visible and holds info about that user.
If nothing is found in database after 4th character is typed div will do the following:
It will create a form, add two hidden inputs holding data typed and pass it over via submit.
add_form.submit()
Now problem is that the form doesn't submit!!
I stay on the same page, am I not supposed to be rendered to page new_clients.html?

What am I missing here?

the javascript:
function add_client_form_submit()
{    
    var add_form = document.createElement("form");
    add_form.setAttribute("method", "get");
    add_form.setAttributes("action", "new_clients.html");

    
            var hiddenField1 = document.createElement("input");
            hiddenField1.setAttribute("type", "hidden");
            hiddenField1.setAttribute("name", "new_name");
            hiddenField1.setAttribute("value", document.getElementById('name') );

            var hiddenField2 = document.createElement("input");
            hiddenField2.setAttribute("type", "hidden");
            hiddenField2.setAttribute("name", "new_phone");
            hiddenField2.setAttribute("value", document.getElementById('phone') );
            
            add_form.appendChild(hiddenField1);
            add_form.appendChild(hiddenField2);
         
    

    document.body.appendChild(add_form);
    add_form.submit();
}

Open in new window


the code that calls it:
document.getElementById('suggested').innerHTML = "<div style='width:300px;' id='line1' > <input type=\"submit\" name=\"add_client\" value=\"client does not exist, add?\" style=\"width:auto;\" onclick=\"add_client_form_submit()\"></div>";	 

Open in new window


this is triggered when no data found in database!
LVL 10
aboo_sAsked:
Who is Participating?
 
David S.Commented:
When you're setting the value of the hidden inputs, you're not setting it to the value of the other inputs, you're attempting to set it as a reference to those inputs.
hiddenField1.setAttribute("value", document.getElementById('name').getAttribute("value") );

Open in new window

In HTML forms you can use the old "DOM0" properties instead of using the getAttribute() and setAttribute() methods (first defined in the DOM1 specs).
hiddenField1.value=document.getElementById('name').value;

Open in new window

Since you're using the GET method anyway, why bother with creating a new form? Why not just generate the query string directly?
function add_client_form_submit()
{    
    location.href="new_clients.html?new_name="+document.getElementById('name').value+"&new_phone="+document.getElementById('phone').value;
}

Open in new window

Another possible problem is that the two input elements you want the values of might have names but not IDs.
0
 
aboo_sAuthor Commented:
Thank you all for your help , I will try your solutions as soon as I could!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.