Page 1 of 1

inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 6:51 am
by s3s3
I am new to this, and after I've searched a bunch of forums and tried it on my own I didn't succed in accomplishing what I wanted, i.e. :

I want to have a text field, in which I will write a number. Then i want a Button to have a MM_goToURL on click action to a Link which has a variable in it with the value from the input text field.

To be more precise here is a part of the code:

the MM_goToURL function:

<script type="text/javascript">
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args+".location='"+args[i+1]+"'");
}
</script>

My text field:

<input name="12" type="text" id="ip" value="1" />

My Button:

<input name="3" type="submit" id="GO" onclick="MM_goToURL('parent','1.1.1.my_var');return document.MM_returnValue" value="Submit" />
<label for="1"></label>

So how can I declare my_var so it will always take the value inside the Input text field, and how do I include that variable inside the link, because just plain writing the name of the variable in there like :MM_goToURL('parent','1.1.1.my_var') won't work.

Thank you.

Re: inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 8:32 am
by social_experiment
:) This looks like a javascript question;

I'm not a expert in it's use but if you can probably access the value of the textfield with this: yourFormId.fieldName.value . I see you use a name for the field consisting of an integer; i'm not sure if this will affect the javascript.
Hth

Re: inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 11:07 am
by s3s3
so smth like
var my_var = ip.12.value; ?

still, how do I write the variable inside the link, in order for the link to read the value inside the variable and not the name of the variable.

Re: inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 3:19 pm
by social_experiment
s3s3 wrote:which has a variable in it with the value from the input text field
Just to be sure i understand what you have in mind: If '5' is entered into the text box, what will the link look like?
s3s3 wrote:var my_var = ip.12.value; ?

That should work yes; the example i pasted uses the id value of the form instead of the id value of the textbox

Re: inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 3:54 pm
by s3s3
if 5 is inserted in the box the link should be 1.1.1.5 and by clicking the button you should be redirected to that ip

so the text field is for the user to input the value of the last 8 bits of an IP address
so if he wants to connect to a device with 1.1.1.200 all he has to do is input the number 200 in the text field and press the button

Re: inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 4:35 pm
by social_experiment

Code: Select all

var valueX = document.getElementById('ip').value;
This will return the value entered inside the textbox with the id of 'ip';

Re: inserting a variable into a MM_goToURL link

Posted: Wed Feb 29, 2012 5:45 pm
by s3s3
ok so I get this
var valueX = document.getElementById('ip').value;

but how do I call that variable from inside the link

writing '1.1.1.valueX' doesnt wok; that was my main concern :)

Re: inserting a variable into a MM_goToURL link

Posted: Thu Mar 01, 2012 12:08 am
by social_experiment

Code: Select all

<script>
function showTheValue(val)
{
	alert(val);
}

function showVal() {
	var valueX = document.getElementById('ip').value;
	
	return valueX;
}
</script>
</head>
<body>
<form method="post">
<input name="12" type="text" id="ip" value="1" />
<input name="3" type="submit" id="GO" onclick="showTheValue(showVal());" value="Submit" />
I think you can call a function which returns the value of the textbox; this is a simple example i scripted; i think the idea behind it should help you with finding an answer

Re: inserting a variable into a MM_goToURL link

Posted: Thu Mar 01, 2012 5:18 am
by s3s3
I must have expressed myself wrong when I said Call.
So this puts the value in the ID with the name IP inside the variable valueX

function showVal() {
var valueX = document.getElementById('ip').value;

return valueX;

My problem is that I cannot just write the variable valueX just like that into the link like :
onclick="MM_goToURL('parent','1.1.1.valueX') because then my link will be 1.1.1.valueX
I also tried
onclick="MM_goToURL('parent','1.1.1.'+valueX) still the button does nothing
onclick="MM_goToURL('parent','1.1.1.'+$('#ip').val()) still the button does nothing
it seems like the function MM_goToURL breaks if you write anything after 'parent','1.1.1.' ; that makes me thinking that maybe the function must be editted?

function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args+".location='"+args[i+1]+"'");
}
to allow certain things to be added

also I tried the following
onclick="MM_goToURL('parent','1.1.1.+$('#ip').val()') it breaks at the red apostrophes
onclick="MM_goToURL('parent','1.1.1.+$(#ip).val()') then the button works but the address I am directed to is 1.1.1.+%24%28
onclick="MM_goToURL('parent','1.1.1.$(#ip).val()') I get 1.1.1.%24%28
onclick="MM_goToURL('parent','1.1.1.$(#ip).val') I get 1.1.1.%24%28
onclick="MM_goToURL('parent','1.1.1.(#ip).val()') I get 1.1.1.%28
onclick="MM_goToURL('parent','1.1.1.(ip).val()') I get 1.1.1.%28ip%29.val%28%29
onclick="MM_goToURL('parent','1.1.1.#ip.val()') I get 1.1.0.1 also if I try onclick="MM_goToURL('parent','1.1.1.') I get 1.1.0.1
If i try onclick="MM_goToURL('parent','1.1.') I get 1.0.0.1
It seems that anything that's written after # is ignored ...

any other brilliant ideas ?:)

Re: inserting a variable into a MM_goToURL link

Posted: Thu Mar 01, 2012 5:57 am
by social_experiment
s3s3 wrote:that makes me thinking that maybe the function must be editted?
Edit the function; my knowledge of javascript won't be able to help solve this, good luck with it though :)

Re: inserting a variable into a MM_goToURL link

Posted: Thu Mar 01, 2012 7:49 am
by s3s3
I also came across this link http://www.phpfreaks.com/forums/index.p ... c=173201.0 where a similar problem exists and only 1 answer that wasn't reported to be good or bad, answer which I don't quite understand. If anyone can take a look there and give his opinion on the matter I'd appreciate it.

Re: inserting a variable into a MM_goToURL link

Posted: Thu Mar 01, 2012 1:13 pm
by s3s3
I got it working but with another function not MM_gotoURL

<script type="text/javascript">
function doit1() {
var x = document.getElementById('ip1').value
window.open('http://1.1.1.'+x)
}
</script>

<input type="submit" name="first" id="first" onclick='doit1()' value="Go to Board" />
<input name="ip" type="text" id="ip1" value="" size="10"/>

Re: inserting a variable into a MM_goToURL link

Posted: Thu Mar 01, 2012 3:39 pm
by social_experiment
Do you have to use the MM_goToUrl function