inserting a variable into a MM_goToURL link

HTML, CSS and anything else that deals with client side capabilities.

Moderator: General Moderators

Post Reply
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

inserting a variable into a MM_goToURL link

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: inserting a variable into a MM_goToURL link

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

Re: inserting a variable into a MM_goToURL link

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: inserting a variable into a MM_goToURL link

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

Re: inserting a variable into a MM_goToURL link

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: inserting a variable into a MM_goToURL link

Post 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';
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

Re: inserting a variable into a MM_goToURL link

Post 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 :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: inserting a variable into a MM_goToURL link

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

Re: inserting a variable into a MM_goToURL link

Post 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 ?:)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: inserting a variable into a MM_goToURL link

Post 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 :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

Re: inserting a variable into a MM_goToURL link

Post 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.
s3s3
Forum Newbie
Posts: 9
Joined: Wed Feb 29, 2012 6:49 am

Re: inserting a variable into a MM_goToURL link

Post 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"/>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: inserting a variable into a MM_goToURL link

Post by social_experiment »

Do you have to use the MM_goToUrl function
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply