PDA

View Full Version : How to do this?



ivytony
02-21-2008, 01:45 AM
I'm designing a form for users to submit. Basically, the form allows users to enter rebate form links. However, the number of rebates varies. So I'd like to have user enter a number first, then a list of input text fields appear underneath the number entered.

I am wondering how to do this???

thanks a lot!!

TimFA
02-21-2008, 02:30 AM
I suppose the easiest method would be with Ajax (XMLHttp to access the...), PHP (the MySQL functions, to mess with the database), MySQL (well its the DB) I don't know how ( or if its possible) to connect to a DB with Ajax. Try checking them out, a combination of them wouldn't be hard. The form could be:



<input type="text" name="rebatepart1" onChange="check();">


Then the Ajax would be something like:



if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

function RSchange() {
if (xmlhttp.readyState==1) {
document.getElementById('contentdiv').innerHTML="Loading..."
}
else if (xmlhttp.readyState==4) {
document.getElementById('contentdiv').innerHTML=xmlhttp.responseText
}
}
function check() {
var data="rebatepart1=" + rebatepart1;
var rebatepart1=getElementByName("rebatepart1");

if (xmlhttp) {
d=document
xmlhttp.open("POST", "somephp.php", true);
xmlhttp.onreadystatechange=RSchange
xmlhttp.send(data)
}
}


The PHP and DB are up to you. Let me know if you need help,

Tim

ivytony
02-21-2008, 02:36 AM
thanks a lot for the code.

I doubt if I need to connect to mysql database, because the number of extra input fields depends on the number entered, ,right?

Here's my HTML code:


<input type="text" name="number_rebates" value="how many rebates?"> //user enters number of extra fields here, then extra input fields shall be generated like the one below with different names (field1, field2, field3, etc).


<input name="field1" type="text" value="rebate1" size="30" maxlength="150">

Thank you!

TimFA
02-21-2008, 04:51 AM
I'm not sure I get what you are saying. Are you saying you want a box for them to tell how many rebates they have? I misunderstood and though you meant have a box where you type in the first number then depending on that number a box appeared below. I didn't quite see the logic in it, but hey your idea not mine. Anyways, if thats what you meant, then you'd best wait for someone who knows JavaScript well, no reason for me to give you an overly complicated PHP/Ajax script to accomplish something JavaScript can do. But, I'm not familiar with JavaScript (its similar to PHP, but I always fail at it). Sorry, good luck.

Tim

EDIT: I thought you mean't you type in the first number (say 2) then something is updated and depending on what number say you could have a box below with something special about it, then at the end submit and whatever your doing collects them all and assembles it into a number or whatever.

ivytony
02-21-2008, 05:02 AM
exactly, you got what I'm trying to say here:

having a user to enter a number first, say he has 3 rebates. Then three input text fields will be generated on the same page without redirecting him. If you know how to do this with AJAX, that would be very nice if can show me how to implement it.

Thanks, I appreciate it.

Tony

TimFA
02-21-2008, 05:08 AM
See the problem is, I'm not familiar with using JavaScript/Ajax to do the writing to the page. However using a Ajax request and a PHP while() auto decreasing loop I can generate fields as field 1,2,3,4 BUT it would be much more efficient to not use PHP. I hate to give you something overly complicated. However, if you want it I'd be more than happy to make it for you.

ivytony
02-21-2008, 06:06 AM
yes, I will appreciate your code. I like AJAX, because it loos cool.

Thanks a lot!!! I appreciate your time and help ;)

TimFA
02-21-2008, 06:53 AM
Sure man.

Attached is a PHP/HTML page, I had to remove the Ajax some reason it refused to send the data to the PHP...anyways, this should do. :) You need to submit, but everything appears to work.

Tim