2

I have tried this code for sending some information to a specific IP. That IP is a microcntroller that acts as a server.

However it sends the information to a page named with that IP not to that IP.

The code is written in JavaScript. What I should to do? Use post method or Xmlhttprequest and how to do that. I think my code is very simple:

<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="192.168.1.250" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>

</html>
1
  • for the record, try: <input required name="fname"> Commented Mar 9, 2013 at 13:09

2 Answers 2

1

You need to include the protocol

action="http://192.168.1.250"
Sign up to request clarification or add additional context in comments.

Comments

0

If you were wanting to send the User to that IP as well, then you would use POST. If actually you want to remain on the same page, send information - visa versa then indeed an AJAX call would be most sufficient. Below I use vanilla JavaScript instead of any JavaScript libraries, although using jQuery would provide you with some callbacks/helpers to make your code more stable.

jsFiddle: http://jsfiddle.net/atjBQ/3/

<script>
    /** 
     * Validate Form, else, Send Ajax
    **/
    function validateform() {
       var x = document.forms["myForm"]["fname"].value;
        if ( x == null || x == "" ) { 
            alert( "First Name must be filled out" );  
            return false;
        }

        /** 
         * If POST
         *   use: xmlhttp.setRequestHeader(
         *           "Content-type", 
         *           "application/x-www-form-urlencoded"
         *           );
        **/
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "http://192.168.1.250?q=" + x, true);
        xmlhttp.send();
        return false;
    }
</script>

<form name="myForm" id="myformtosend">
    <label for="fname">First name:</label><input type="text" name="fname" />
    <input type="submit" value="Submit" />
</form>

With jQuery:

/** 
 * Snippet Reference to: 
 * http://api.jquery.com/jQuery.post/   
**/

<script>
   $.ajax({
     type: "POST",
     url: "http://192.168.1.250",
     data: data,
     success: function() {
        /** Some Code **/
     }   
   });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.