0

I want to enter value, to input field so when I leave the input field by clcking out side from the input field I will run the ajax code and send the value input dataString to same file which in this case cald ajax.php. Thanks

my code:

$(document).ready(function()
{



    var dataString;

$("valueforajax").mouseleave(function() {//when  we leave the field.
    alert('test it is mouseup ');
    dataString=$("#valueforajax").val();
    alert(dataString);
  if ("" = dataString){//it is mean that input field not empty
        $.ajax({
        type: "POST",
         url: "../../ajax.php",
             data: dataString,
             cache: false,
             success: function(html)
             {
             alert("There is submited sucsses");
             }
             });//ajax
        }//if
    });//mouseup
});//ready

ajax.php

<?php
    if  (isset($_POST['dataString'])){
        echo ("dataString not empty:= ".$_POST['dataString']);}
    ?>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="./public/stylesheets/stylesheets.css"  >
    <script type="text/javascript" src="./public/js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="./public/js/ajax.js"></script>

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
    <title>mange panel</title>
    </head>
    <body>

    <br>Type value for test in ajax<input id="valueforajax" type=text name='ajaxtest'>


    </body>
    </html>

3 Answers 3

3

You missed # sign in you selector

$("valueforajax") should be $("#valueforajax").

also

data: dataString should be data: 'dataString=' + dataString OR data: {"dataString": dataString} because in your php code you're searching for

$_POST['dataString'] i.e. for dataString key.

$("#valueforajax").blur(function() { // blur is perfect for what you searching

    dataString = $.trim(this.value); // you don't need $("#valueforajax").val(); 
                                     // here, this is enough

  if (dataString){  // checking for presence of value
        $.ajax({
        type: "POST",
         url: "../../ajax.php",
             data: 'dataString=' + dataString, // or {"dataString": dataString}
             cache: false,
             success: function(html) {
                alert("There is submited sucsses");
             }
          });
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

You want to use blur instead of mouseleave.

A blur event occurs whenever you leave a form element. It occurs when you hit TAB, click outside the field, change fields, etc....

mouseleave only occurs when your mouse literally leaves the element.

Comments

0

The data must be an object (eg {"dataString" : "test"} ) or a string (eg "dataString=test")

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.