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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cant get this javascript to parse right!

    I am having major problems with this code:


    code:

    Code:
    function GetFormHTML()
    {
    	thiscount = counter();
    	var htmlStr = '';
    	htmlStr += '<table><tr><td>Sensor Type</td><td>                    Address</td><td>                       Formula</td></tr></table>';
    	for (var i=0;i<gFieldValues.length;i++)
    			htmlStr += '<table><tr><td><input type="text" name="myoptions" value="'+ getFieldValue[i] +'"><input type="hidden" value="<?php $_POST[myoptions[thiscount]];?>"><input type="hidden" value="<?php $_POST[thiscount];?>"> <input type="text" name="multifield" value="' + gFieldValues[i] + '"> <input type="text" name="address" value="'+ gFieldValue[i] + '"></td><tr></table><input type="hidden" name="thiscounter" value="' + thiscount + '">';
    	htmlStr += '<input type="button" value="New Type" onClick="AddField()">';
    
    	return htmlStr;
    
    }
    Php code
    Code:
    $blah=$_POST['thiscounter'];	
    	$this=$_POST['myoptionpass'];
    	for($z=0; $z<$blah; $z++)
    	{
    
    		print($this[$z]);
    
    	}
    What I need to to do,is when i hit a submit button at the end it sends the values over to a php page, and that php code is used to get the values (of myoptions right now) as each field is added dynamically. The counter itself passes fine, but I cant get each individual myoptions to go through. Does anyone see what I am doing wrong? Do I need to create some sort of new array to hold the field values through each pass? I am at a loss and need to get this done asap! Please help! Thanks!
    PS...the javascript is on one page, and the php on another
    Also note that these arrays are dynamic, so doing like upwards of 500 static things to hold the vars isnt viable. I know its an array problem, I just cant figure it out! BTW im new here, hiiiiii

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try changing:
    name="myoptions"

    to:
    name="myoptions[]"

    and see how that works out for you.

    Hope that helps,
    Sadiq.

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function AddField()
    {
    	
    	// Save previously entered data here
    	var formObj = GetFormObj();
    
    	for (var i=0;i<gFieldValues.length;i++)
    	{
    		if (gFieldValues.length>1)
    			gFieldValues[i]= formObj.multifield[i].value;
    
    		else
    			gFieldValues[i]= formObj.multifield.value;
    	
    	}
    
    	for (var j=0;j<gFieldValue.length;j++)
    	{
    		if(gFieldValue.length>1)
    			gFieldValue[j]=formObj.address[j].value;
    
    		else
    			gFieldValue[j]=formObj.address.value;
    	}
    
    	for (var k=0;k<getFieldValue.length;k++)
    	{
    	if(getFieldValue.length>1)
    		getFieldValue[k]=formObj.myoptions[k].value;
    	else
    		getFieldValue[k]=formObj.myoptions.value;
    	}
    	
    	
    	// Create the new field
    	gFieldValues[gFieldValues.length]="";
    	gFieldValue[gFieldValue.length]="";
    	getFieldValue[getFieldValue.length]="";
    	UpdateForm();
    
    }
    doesnt work becuase of that...any other hints ?

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't get it. What doesn't work because of what?

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check out the php in the top post there. Whats not working is the myoptions field isnt passing to the php page correctly. I can get like the very last data entered into the last field but that is it. I havent been able to get the array working right so that, say i have like 10 fields added in total, that the 10 different things show up instead of just the very last one. Thats what I dont get :-/

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What's myoptionpass?

    Hear me out: what I'm proposing is that in your input's names should be myoption[]. Then in your PHP, try accessing the array like so:
    PHP Code:
    foreach($_POST["myoption"] as $index => $val) {
     print(
    "myoption[".$index."] = ".$val."<br>");

    That should do it.

    Let me know what happens,
    Sadiq.

  • #7
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I hear what your saying, but the code i have wont allow for myoptions[] because of the way its retreiving everything. see that other code i posted like two posts up there.

    If i can get that to go with where it retains the values and adds a new line i think its all good to go, thats what i cant figure out otherwise, otherwise your suggestion does seem to be working.

    It tells me in the error when i try to hit the add type button that myoptions.value is null or not an object.
    Last edited by jediman; 03-11-2004 at 08:33 PM.

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is this stuff online anywhere? Because I don't really understand your could.

    You make it sound like you're creating new fields dynamically, I but I don't see any DOM stuff. You're making reference to a lot of other variables and functions, and I don't know what they do.

    So I'm sorry that I can't be of further help because I don't understand what's going on, what the errors are and what they mean in this context (because I don't see enough to know what the context is) to help you.

    So until you provide more code I'm at a loss. It almost seems to me that you may have to re-work a bunch of your code to accomodate this.

    Sadiq.

  • #9
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script LANGUAGE="JavaScript">
    var mycount = 0;
    function counter()
    {
    
    mycount++;
    
    return mycount;
    }
    
    
    // Browser Detection
    isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
    NS4 = (document.layers) ? true : false;
    IEmac = ((document.all)&&(isMac)) ? true : false;
    IE4plus = (document.all) ? true : false;
    IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
    IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
    ver4 = (NS4 || IE4plus) ? true : false;
    NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
    
    // Body onload utility (supports multiple onload functions)
    var gSafeOnload = new Array();
    function SafeAddOnload(f)
    {
    
    	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
    	{
    		window.onload = SafeOnload;
    		gSafeOnload[gSafeOnload.length] = f;
    	}
    	else if  (window.onload)
    	{
    		if (window.onload != SafeOnload)
    		{
    			gSafeOnload[0] = window.onload;
    			window.onload = SafeOnload;
    		}		
    		gSafeOnload[gSafeOnload.length] = f;
    	}
    	else
    		window.onload = f;
    }
    function SafeOnload()
    {
    	for (var i=0;i<gSafeOnload.length;i++)
    		gSafeOnload[i]();
    
    	for (var j=0;j<gSafeOnload.length;j++)
    				gSafeOnload[j]();
    
    	for (var k=0;k<gSafeOnload.length;k++)
    				gSafeOnLoad[k]();
    
    
    }
    
    
    //
    // Main Form Functions
    //
    
    // This array holds our form values when we need to regenerate the form
    var gFieldValues = new Array(1);  
    
    var gFieldValue = new Array(1);
    
    var getFieldValue = new Array(1);
    
    
    
    for (var i=0;i<gFieldValues.length;i++)
    	gFieldValues[i]="";
    
    for (var j=0;j<gFieldValue.length;j++)
    	gFieldValue[j]="";
    
    for (var k=0;k<getFieldValue.length;k++)
    	getFieldValue[k]= "";
    
    
    function GetFormHTML()
    {
    	thiscount = counter();
    	var htmlStr = '';
    	htmlStr += '<table><tr><td>Sensor Type</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Address</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Formula</td></tr></table>';
    	for (var i=0;i<gFieldValues.length;i++)
    			htmlStr += '<table><tr><td><input type="text" name="myoptions[]" value="'+ getFieldValue[i] +'"><input type="hidden" value="<?php $_POST[myoptions[thiscount]];?>"><input type="hidden" value="<?php $_POST[thiscount];?>">&nbsp;<input type="text" name="multifield" value="' + gFieldValues[i] + '">&nbsp;<input type="text" name="address" value="'+ gFieldValue[i] + '"></td><tr></table><input type="hidden" name="thiscounter" value="' + thiscount + '">';
    	htmlStr += '<input type="button" value="New Type" onClick="AddField()">';
    
    	return htmlStr;
    
    }
    
    
    function GetFormObj()
    {
    	var returnObj = null;
    	
    	if (IE4plus)
    	{
    		returnObj =  document.dynoform;
    	}
    	else if (NS4)
    	{
    		returnObj =  document.formlayer.document.dynoform;
    	}
    	else if (NS6)
    	{	
    		returnObj =  document.getElementById("dynoform");
    	}
    	return returnObj;
    }
    
    function AddField()
    {
    	
    	// Save previously entered data here
    	var formObj = GetFormObj();
    
    	for (var i=0;i<gFieldValues.length;i++)
    	{
    		if (gFieldValues.length>1)
    			gFieldValues[i]= formObj.multifield[i].value;
    
    		else
    			gFieldValues[i]= formObj.multifield.value;
    	
    	}
    
    	for (var j=0;j<gFieldValue.length;j++)
    	{
    		if(gFieldValue.length>1)
    			gFieldValue[j]=formObj.address[j].value;
    
    		else
    			gFieldValue[j]=formObj.address.value;
    	}
    
    	for (var k=0;k<getFieldValue.length;k++)
    	{
    	if(getFieldValue.length>1)
    		getFieldValue[k]=formObj.myoptions[k].value;
    
    	else
    	getFieldValue[k]=formObj.myoptions.value;
    
    
    
    	}
    	
    	
    	// Create the new field
    	gFieldValues[gFieldValues.length]="";
    	gFieldValue[gFieldValue.length]="";
    	getFieldValue[getFieldValue.length]="";
    	UpdateForm();
    
    }
    
    
    function UpdateForm()
    {
    	var htmlStr = GetFormHTML();
    	if (IE4plus)
    	{
    		document.all.formlayer.innerHTML = htmlStr;
    	}
    	else if (NS4)
    	{
    		document.formlayer.document.open();
    		document.formlayer.document.write(htmlStr);
    		document.formlayer.document.close();
    	}
    	else if (NS6)
    	{	
    		document.getElementById("formlayer").innerHTML = htmlStr;
    	}
    }
    
    function IncludeForm()
    {
    	
    	var htmlStr = GetFormHTML();
    
    	if (IE4plus || NS6)
    	{
    		document.write('<DIV ID=formlayer name=formlayer  STYLE="position:relative; WIDTH=400px; HEIGHT=50px">' + htmlStr + '</DIV>');
    	}
    	else if (NS4)
    	{
    		// Because NS needs floating layers, we need a placeholder graphic to force anything
    		// below the layer content to leave whitespace for the layer.  The position of this
    		// graphic is also used in determining the position of the layer.
    		document.write('<img name="formlocation" border="0" width="400" height="200" src="images/spacer.gif">');
    
    	}
    
    }
    
    //
    // Netscape 4.x Ineptness
    //
    function HandleOnload()
    {
    	if (NS4)
    	{
    		var width = document.formlocation.width;
    		var height = document.formlocation.height;
    		
    		nL=new Layer(width);
    		nL.name = "formlayer";
    		nL.left=document.formlocation.x;
    		nL.top=document.formlocation.y;
    		nL.bgColor = "white";
    		nL.clip.width=width;
    		nL.clip.height=height;
    		nL.document.open();
    		nL.document.write(GetFormHTML());
    		nL.document.close();
    		nL.visibility = 'show';
    
    		document.formlayer = nL;
    
    	}
    }
    
    function HandleResize()
    {
    	location.reload();
    	return false;
    }
    
    if (NS4)
    {
    	SafeAddOnload(HandleOnload);
    	window.captureEvents(Event.RESIZE);
    	window.onresize = HandleResize;
    }
    
    
    </script>
    theres the javascript

  • #10
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any takers? I need to get this going as soon as humanly possible, and im at a lost, and I am hoping someone could help :-/

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are only getting the last entry of the gFieldValues array.
    Your script is not stepping thru the array properly.
    Try this:
    function GetFormHTML(){
    thiscount = counter();
    var htmlStr = '';
    htmlStr += '<table><tr><td>Sensor Type</td><td> Address</td><td> Formula</td></tr></table>';

    var i = 0;
    while (
    i<gFieldValues.length){
    htmlStr += '<table><tr><td><input type="text" name="myoptions" value="'+ getFieldValue[i] +'"><input type="hidden" value="<?php $_POST[myoptions[thiscount]];?>"><input type="hidden" value="<?php $_POST[thiscount];?>"> <input type="text" name="multifield" value="' + gFieldValues[i] + '"> <input type="text" name="address" value="'+ gFieldValue[i] + '"></td><tr></table><input type="hidden" name="thiscounter" value="' + thiscount + '">';
    htmlStr += '<input type="button" value="New Type" onClick="AddField()">';

    return htmlStr;
    }
    }


    .....Willy

    Edit: Removed my unneeded changes to the lenth.
    Last edited by Willy Duitt; 03-12-2004 at 03:41 PM.

  • #12
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still doesnt work, with that -1 the fields dont even show for one thing, and then even before that its telling me that multified[...] is like null or not an object. IT was working except for the part down wher eit gets and stores the values every time you click add type, since it complained about the myoptions.value being null or not an object.
    Code:
    function AddField()
    {
    	
    	// Save previously entered data here
    	var formObj = GetFormObj();
    
    	for (var i=0;i<gFieldValues.length;i++)
    	{
    		if (gFieldValues.length>1)
    			gFieldValues[i]= formObj.multifield[i].value;
    
    		else
    			gFieldValues[i]= formObj.multifield.value;
    	
    	}
    
    	for (var j=0;j<gFieldValue.length;j++)
    	{
    		if(gFieldValue.length>1)
    			gFieldValue[j]=formObj.address[j].value;
    
    		else
    			gFieldValue[j]=formObj.address.value;
    	}
    
    	for (var k=0;k<getFieldValue.length;k++)
    	{
    	if(getFieldValue.length>1)
    		getFieldValue[k]=formObj.myoptions[k].value;
    
    	else
    	getFieldValue[k]=formObj.myoptions.value;
    
    
    
    	}
    	
    	
    	// Create the new field
    	gFieldValues[gFieldValues.length]="";
    	gFieldValue[gFieldValue.length]="";
    	getFieldValue[getFieldValue.length]="";
    	UpdateForm();
    
    }
    thats what im talkinga bout. If i change myoptions to myoptions[] to step trhough which dOES work if it stops complaining that is... i cant see how to fix it from saying that myoptions.value is null or not an object...
    Last edited by jediman; 03-12-2004 at 03:27 PM.

  • #13
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then perhaps it is time to puplish a test page so someone can actually run your codes and see what is happening. If you were getting the value of the last gFieldsValue then the while loop should have resolved your problem of missing the others.

    ......Willy

  • #14
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #15
    New Coder
    Join Date
    Mar 2004
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Plus in that code you posted, where does i increment ? I tried using i++; but with and without it i get errrors. Let me know if youve taken a look at the page.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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