Jump to content

[SOLVED] "Add another x" (add another form element(s)) button


Catfish

Recommended Posts

Hi all,

 

I haven't used Javascript much mainly PHP, but I want to add some funky functionality to my page and need one of those form buttons you press to add another (sometimes a set of) form elements to the page because there is more data you want to input.

 

Examples of this are when people upload photos to a website, there is generally a button that says "add another file" or something and when you click it all the form elements for another file to upload appears on the page.

 

I tried Googling for it, but I don't think I could find the right words to describe what I want. Also couldn't find it in this forum so I'm sorry if this is a common question.

 

Thanks for any help.

Link to comment
Share on other sites

Ok, I know I posted this solved, but now I have just tried some input with my forms and if I add one input field and type in it then click to add another input field the value i typed in the first one disappears! And so on everytime you add another field.

 

How can I stop this from happening?

 

Here is my JS code:

<script type="text/javascript">
function addInput()
{
var y = document.getElementById("nextID");
var z = y.value;
y.value++;
var x = document.getElementById("inputs");
x.innerHTML += "Field " + z + " - Name: <input type=\"text\" name=\"fieldName[" + z + "]\" />";
x.innerHTML += " Type: <select type=\"dropdown\" name=\"fieldType[" + z + "]\">'.addslashes(outputSQLiteTypes()).'</select>";
x.innerHTML += " Extras: <input type\"text\" name=\"fieldExtras[" + z + "]\" /><br />";
}
</script>

 

and this is the PHP/HTML that calls it:

 

<form action="'.$_SERVER['PHP_SELF'].'" method="POST">.urlAllValues($localPostGet, TRUE)
// post all POST/GET values as well
<input type="button" onmousedown="addInput();" value="Add Field" />
<input type="hidden" id="nextID" value="1" />'."\n".'<div id="inputs"></div>
<input type="hidden" name="action" value="submitTable" />
<input type="submit" value="Create Table" />
</form>

 

Please note that the above HTML is output via PHP, so the PHP function call in there is legitimate, I have just removed all of the quotes for readability.

Link to comment
Share on other sites

The problem of the form fields losing their values has been fixed by using createElement() function. It was a bit more coding, but it hasmade the script much more compatible.

 

The code seen above works in IE but not in Firefox, which is why I noticed the error.

 

Topic is now solved

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.