Jump to content

How do I echo a variable into another variable?


bobleny

Recommended Posts

Hi! I have a form with inputs who's names are like this: boxy_1, boxy_2, boxy_3, boxy_4, etc...

I know that there are 1000 inputs with this name.

 

What I need to do is, print the inputs values. To do this, I use a for loop;

for(var i = 1; i <= 1000; i++)
{
    document.getElementById("message").innerHTML += form.boxy_i.value;
}

 

 

So, you see the problem yet?

The variable "i" is not being echoed into, "form.boxy_i.value". So javascript is looking for a form input named "boxy_i", when it should be looking for an input named "boxy_1", "boxy_2", "boxy_3", "boxy_4", etc...

 

 

My question is, how do I fix this?

Link to comment
Share on other sites

How about we try it like this:

 

<script language="javascript">
function doIt()
{
for (i=1; i<=2; i++)
{
    document.getElementById("message").innerHTML += document.getElementById("boxy"+i).value + " ";
}
}
window.onload=function()
{
doIt();
}
</script>

<form>
<input type="text" id="boxy1" value="Hello">
<input type="text" id="boxy2" value="World">
</form>

<div id="message"></div>

 

Can you live with it this way? :)

 

Of course you will want to change the script around to the way you want it to be; but the above is just for demo purposes. ;)

Link to comment
Share on other sites

 

It works fine for me; it is getting all the values from your input fields, that have an id of "boxy"; plus the corresponding number, that goes along with the id. The form is not even in play with this script; it is pulling values ("echo"ing is you call it; but that is a PHP term) from input fields that have the "boxy" (plus number) id and displaying the values in your "message" div. Doesn't refresh the page for me; that issue must be coming from somewhere else in you script/page. Did you add the ids to each of the input fields; like the way I showed you in the demo?

Link to comment
Share on other sites

 

Here is another example of how it would work, but let me warn you now; if your trying to get the values of this many input fields at one time; your going to freeze up people's browsers (due to all the memory this script would take up; once it initiated).

 

<script language="javascript">
function doIt()
{
for (i=1; i<=1000; i++)
{
document.getElementById("message").innerHTML += document.getElementById("boxy"+i).value + " ";
}
}
</script>

<form>
<script language="javascript">
for (i=1; i<=1000; i++) {
document.write('<input type="text" id="boxy'+i+'" value="Hello">');
}
</script>
<br><br>
<input type="button" onclick="doIt()" value="Return Values">
</form>

<div id="message"></div>

Link to comment
Share on other sites

Well, this is how the script works. A user enters a directory and clicks submit. This then calls a PHP script that creates a form. It then uses ajax to echo the form on to the current page. This form is a list of check boxes.

 

As far as I know, a check box's can't carry a string value, so I use a hidden field to hold the values.

 

The user then checks the boxes next to the file that they want to delete. I got all of that....

 

Next, I need to determine which boxes are checked. Then I need to send the value of the coinciding hidden field to another php script that will delete the appropriate files.

 

Here is the script:

<html>
<head>
	<title>Data Deleter!</title>
	<script type="text/javascript">
		<!--
			var GetServer
			function Connect()
			{
				try
				{
					// Firefox, Opera 8.0+, Safari
					GetServer = new XMLHttpRequest();
				}
				catch (e)
				{
					// Internet Explorer
					try
					{
						GetServer = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch (e)
					{
						try
						{
							GetServer = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch (e)
						{
							return false;
						}
					}
				}
			}

			function Make(info)
			{
				Connect();
				GetServer.onreadystatechange=function()
				{
					if(GetServer.readyState == 4)
					{
						document.getElementById("sep").innerHTML = "<hr align='left' width='37%' />";
						document.getElementById("message").innerHTML = GetServer.responseText;
					}
				}

				GetServer.open("GET","file_seek.php?loc="+info.loc.value, true);
				GetServer.send(null);

				document.formy.loc.focus();
				return false;
			}

			function checkAll(value)
			{
				var inputs = form.getElementsByTagName('input');
				for (var i = 0; i < inputs.length; i++)
				{
					if (inputs[i].type == "checkbox")
					{
						inputs[i].checked = value;
					}
				}
			}

			// info is there just in case I need it...
			function Delete(info)
			{
				for(var i = 1; i <= form.found_files.value; i++)
				{
					// At the moment, I am simply trying to get the correct data
					//    to be displayed for visual inspection.
					document.getElementById("message").innerHTML += form.boxy_i_value.value +"<br />";
				}

				return false;
			}

			function focus()
			{
				document.formy.loc.focus();
			}
		//-->
	</script>
</head>
<body onload='focus()'>
	<form name='formy' onsubmit='return Make(this)' method='post'>
		Directory: <input type='text' name='loc' size='50' />
		<hr>
		<input type='Submit' value='Send'>
	</form>
	<div id='sep'></div>
	<div id='message'></div>
	<div id='sep2'></div>
</body>
</html>

 

By echoed, I mean PHP is literally echoing this form line by line for ajax to receive....

And here is the echoed form:

<form name='form' onsubmit='return Delete(this)' method='post'>
    <input type='checkbox' id='checkbox' name='boxy_1' />
    <input type='hidden' id='hidden' name='boxy_1_value' value='someLink' />

    <input type='checkbox' id='checkbox' name='boxy_2' />
    <input type='hidden' id='hidden' name='boxy_2_value' value='somLink' />
    
    <input type='hidden' name='found_files' value='1000' />
    <input type='checkbox' name='all' onclick='checkAll(this.checked)' />Select/Deselect All (1000) Items
    <input type='Submit' value='Delete Selected Items'>
</from>

-------------------------

 

Now, I don't care how I accomplish this in terms of programming, I just would like to get this done...

 

P.S. 1000 files is crazy, there never actually be that many, but you never know....

Link to comment
Share on other sites

Oh my god, I got it to work!

 

I was bored waiting for a reply, so I was reading Mike Kruse's javascript dos and doents. and he started to talk about dot notation and square bracket notation. As it turns out...

This:

form.boxy_1

 

is the same as this:

form["boxy_1"]

 

Which allows me to do this:

form["boxy_"+i].checked

 

and this:

form["boxy_"+i+"value"].value

 

 

I knew there was a way to do it! All that time.... :'(

All well, at least I now know..............

 

Thanks for your help PHP question man!

 

 

---------

P.S. I will click on the "Topic Solved" button as soon as it makes its self avalable... Stupid button...

Link to comment
Share on other sites

It shouldn't matter if it is a checkbox or not... I needed to get the info from both the checkbox and the hidden values. It is very simple to convert the one to the other.

 

The issue was echoing information into a variable from a form. That is really all the information that should be necessary for this situation: form["boxy_"+i].checked

 

Because of that, I do limit the amount of information I give. This, "form["boxy_"+i].checked", is the answer I was looking for. Had I given you the entire situation as I did in reply #10, you may have came up with a solution for my problem. However, that solution would most likely have been something other than, "form["boxy_"+i].checked".

 

So I have to ask you, had I given you all the information that I gave you in Reply #10 at the start, would you have thought of this line?:

form["boxy_"+i].checked

 

 

The reason I eventually did explain the entire situation was because, I figured some solution is better that no solutions....

Link to comment
Share on other sites

 

phpQuestioner wrote you a good script and it does work; so don\'t knock people when they ask you for some info. Otherwise you will not get help from people or the help they give you will not turn out the way you wanted it to be. I guess the later would apply to you, in this case.

Link to comment
Share on other sites

You mean you made an account just to scold me....? That's not cool... :'(

 

I meant no disrespect to any one. I was simply explaining why I don't usually come out with as much detail as possible. And actually, I have found that too much detail can complicate things...

 

phpQuestioner's script did work, I tried it. However, do to the unique situation of my script, his script simply wouldn't work. I actually took his idea and tried it several different ways to suit my needs. It didn't work. I have nothing against phpQuestioner and think he is very good with javascript and I really do appreciate his help.

 

I came forth with the whole script because I began to feel that it was necessary in order to get a different response...

 

I have in the past ask for help with too much information at the start. This can lead to miss understandings, confusions, and many suggestions that may work, but often not quite the way I want them to.

 

So, I give the information that I feel is necessary to get the response I'm looking for.

 

It is far from phpQuestioner's fault that his suggestions didn't work.

 

Again, I am sorry if you felt this was an attack on phpQuestioner. It was not. I also hope that I will be able to count on his help in the future!

 

-----------------

P.S. In case you where wondering, this is how I ended up passing the data on to the PHP file to be deleted!

function Delete()
{
var trail = "?foundFiles="+form.found_files.value;
for(var i = 1; i <= form.found_files.value; i++)
{
	if(form["boxy_"+i].checked == true)
	{
		trail += "&boxy_"+ i +"_value="+ form["boxy_"+ i +"_value"].value;
	}
}

Connect();
GetServer.onreadystatechange=function()
{
	if(GetServer.readyState == 4)
	{
		document.getElementById("message").innerHTML = GetServer.responseText;
	}
}

GetServer.open("GET","file_delete.php"+trail, true);
GetServer.send(null);

timer();

return false;
}

 

Thanks again for all of your help!

 

 

"Now where is that darned Topic Solved button....!"

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.