Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript calendar & tables

Status
Not open for further replies.

GiddyRob

Programmer
Aug 25, 2005
37
GB
Hi,

I have some code that creates calendar objects the problem is that I want the links to the calander stored in a table populated dynamically (using a while loop). The code below shows the field that creates the link button and text field that the date goes into. Basically when the user clicks on the link it brings up a calendar in another window. The user can pick a date and this is put in the textfield.

Code:
<td><form name=\"form\"><span class=\"style2\">&nbsp;<input type=\"Text\" name=\"text\" value=\"\"><a href=\"javascript:call.popup();\"><img src=\"images/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;</span></form></td>

The "call" in the javascript is a calendar object called "call".

Code:
<script language=\"JavaScript\">
  
  var call = new calendar1(document.forms['form'].elements['text']);
   call.year_scroll = true;
   call.time_comp = false;
  

</script>

It uses 'form' & 'text' which are the names of the form and the textfield. I need it to create a different calendar object for each row. At the moment I get an error that says "Error 'document.forms.form.elements.text' is null or not an object". Both links open the same calendar object and it just populates the first textfield in the table.

The image shows what is going on.

calendar.gif


Does anyone know how I can fix this problem so that I can have as many or little calendar objects in the table.

Any help would be great

Cheers

Rob
 
by leaving the form action blank it just refreshes the page and activates the code below because the assign submit button has been pressed. All the buttons in the table will activate the if statement.

Code:
if(isset($_POST['assign'])){//handle the form when the user presses the button

//if submit button 'assign' is pressed do this logic in the curly brackets

The onSubmit is blank because this is going to be used for javascript validation. The form doesn't need this to work but I put it in there to remind me to do it later.

When its in the if statement above (done by pressing the submit button), I need to get the textfield's value. Normally the code below would do as it uses the textfield's name to get its value for the php variable.

Code:
$date=($_POST['textfieldname']);

This won't work now that I have dynamic textfield names. I tried the code below to get it but it still won't work.

Code:
$date=($_POST['mytext'.$row[0]]);

Does this make it clearer? I can send you the whole page again if that helps?

Cheers

Rob


 
You are going to be doing field validation before you do submissions in the future correct?

If so then that is the place for you to set the value.

Do this.
replace:
onSubmit=\"\">
with:
onSubmit=\"formvalidation(".$row[0].")\">

Put this javascript function into the page.

function formvalidation(which)
{
document.selectedfield.value = "mytext" + which;
}

Make sure you have a hidden field with the name of selectedfield. Or, use one that you have and change the name in the javascript function.

Now, your onsubmit command will call the function formvalidation and pass in the number that was used to name that form and it's text field.
The function uses that number to generate the correct field name and store it into your hidden field.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Hi, I have tried your idea but the problem is that the value is passed to the hidden field's value. When I post it it just outputs the value of the hidden field ie mytext1. If I change the document.selectedfield.value to document.selectedfield.name then I guess that would work but the problem then is it is not a static field name for the $_POST function.

Blimey this is wacky!

Any other thoughts???

Cheers once again.

Rob
 
Hi again, I have this that will work if it is possible in javascript to do what I want it to do.
Code:
function gettingthedate(which)
	{
	document.myform1.bobitin.value = document.myform1.mytext1.value;

	}
I need to replace the "1"s with the "which" value for myform1, myform1, mytext1. ie something like myform.which, mytext.which.

Would you know what the syntax for that is?

Cheers

Rob
 
Yes, this is what I was getting at. You are only storing the NAME of the field you need to check in the hidden field.
Once the page loads you check that field for the name so you know which value to go out and grab.

Another approach is to grab the value of the date field rather than it's name and store that. It would save steps in your code but it depends on how you are treating the rest of the form data when the page reloads.

Try this:

function formvalidation(which)
{
var myformfield = "myform" + which;
var mydatefield = "mytext" + which;
document.selectedfield.value = document.myformfield.mydatefield.value;
}


This way you set the actual DATE into the hidden field instead of the name of the date field.
Try this and see if it works. If it does we still need to error check to make sure a value actually exists rather than just that someone hit the submit button without selecting a date.

Paranoid? ME?? WHO WANTS TO KNOW????
 
Hi,

been off for the bank holiday weekend so just back into the comp.

Each row in the table has a form. The myform number depends on the record in the database. ie if jobid = 1 then myform will be myform1 etc.

document.selectedfield.value won't work as it is because it needs to know dynamically what form number it is associated with. e.g.

document.myform???.bobitin.value = etc

don't know how to make the form dynamic, still working on it though.

Got a clue?

Cheers

Rob
 
tried that code but it won't work. I want to be able to get the textfield value into the hidden field value though.

Code:
function gettingthedate(which)
	{
	var myformfield = "myform" + which;
	var mytextfield = "mytext" + which;
	document.myform3.bobitin.value = document.myformfield.mytextfield.value;

	}

can't find any code anywhere that tells me how to append a value onto the end of a form or a textfield. I can make it work statically but thats it. As you can see from the code above I need to make myform3 dynamic aswell.

Cheers

Rob
 
Rob, Try this.
function gettingthedate(which)
{
var newmydatefield = document.all('bobitin');
var mytextfield = "mytext" + which;
var mynewfield = document.all(mytextfield);
newmydatefield.value = mynewfield.value;
}

This line: var newmydatefield = document.all('bobitin');
Gets the object named bobitin and assigns it to the variable newmydatefield.

This line: var mytextfield = "mytext" + which;
Appends the value passed in to the word mytext.

This line: var mynewfield = document.all(mytextfield);
Grabs the object named mytextx (where x is the passed in value) and assigns it to the variable mynewfield.

This line: newmydatefield.value = mynewfield.value;
assigns the value stored in mytextx as referenced through our variable mynewfield to the field bobitin as referenced through our variable newmydatefield.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Hey,

cheers. I have had another think about it and it can be done really easily in PHP.

I just created a hidden input which holds the value of row[0] for each row then did this

Code:
if(isset($_POST['assign'])){//handle the form when the user presses the button

 $hidden=($_POST['bobitin']);	
 $expdate=($_POST['mytext'.$hidden]);
 echo"$expdate";	

}

Jobs a good one.

Cheers for all you help.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top