Need help with passing data to new url.

B

bjorgenson

Here is my dilema: I want a form that includes an input box and submit
button. A user will type in some data in the input box. When they click
submit, I need set variables appended to this input text and then
passed on to the same page "_parent" as a new url.

For example:

The user types in "mycomputer". When submited, I need to add the
following text to the beginning and end of "mycomputer"


http://mycomputer:8000/mysite

Where "http://" would = variable1 and ":8000/mysite" would = variable2

Thanks,
Brian
 
B

bjorgenson

Was able to figure it out:

<html>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function submitForm( Form1 )
{
if ( Form1.newurl.value == "" )
{
alert( "Please enter a valid IP or computer name" );
return;
}

Form1.action = "http://"
+ Form1.newurl.value
+ ":5800";
Form1.submit();

}

// -->
</SCRIPT>

<FORM NAME="myForm" METHOD="post">
<INPUT TYPE="text" NAME="newurl">
<INPUT TYPE="button" VALUE="Connect" ONCLICK="submitForm(this.form);">
</FORM>
</body>
</html>
 
Z

Zif

Was able to figure it out:

No, you didn't...
<html>
<body>
<SCRIPT LANGUAGE="JavaScript">

The language attribute is depreciated, type is required:


Attempting to hide scripts is pointless and possibly harmful, don't bother.
function submitForm( Form1 )
{
if ( Form1.newurl.value == "" )
{
alert( "Please enter a valid IP or computer name" );
return;
}

Form1.action = "http://"
+ Form1.newurl.value
+ ":5800";
Form1.submit();

}

// -->
</SCRIPT>

<FORM NAME="myForm" METHOD="post">
<INPUT TYPE="text" NAME="newurl">
<INPUT TYPE="button" VALUE="Connect" ONCLICK="submitForm(this.form);">

If the user types some text into the input and presses return, the form
is submitted without your onclick running. Put your onclick function
into the form onsubmit event and no matter how the form is submitted, it
will run (provided your user has javascript enabled).

You should provide an alternative if javascript is not working, say a
redirect from your server based on the value returned in the form or at
very least a default action that explains why the form doesn't work.
</FORM>
</body>
</html>

Try:

<script type="text/javascript">

function initForm(f) {
document.forms[f].onsubmit = function() {return submitForm(this); };
}

function submitForm( Form1 ) {
if ( Form1.newurl.value == "" ) {
alert( "Please enter a valid IP or computer name" );
return false;
}
Form1.action = "http://"+Form1.newurl.value+":5800";
}

window.onload = function() { initForm('myform');};

</script>
<form name="myform" action="You_Dont_Have_JS_Enabled.html"
method="post">
<input type="text" name="newurl">
<input type="submit" value="connect">
</form>
 
G

Grant Wagner

I'm no JS expert but your code solves the "onclick" issue. Thanks!

Another thing, can anybody tell me why there is a "-" after my value?
I
am now getting "http://ip-address-:5800"

Because you're appending one? Because you're typing one into the input
box? Without seeing your now "working" code, there's no way to know why
you are constructing the string you are constructing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top