-1

I need to save the value of a input type text in a PHP variable as soon as the user writes it. I found that the blur event in JQuery can trigger an event that happens after writing to the input type text, so I have the following:

<script>
     $("document").ready( function() 
     { 
       $("#primerApellido").blur(function() {
            alert('out');
            <?php 
                $primerApellidoForm = 
                "<script type=\'text/javascript\'>
                    $('#primerApellido').val();
                </script>
                ";
            ?>                
        });
     }); 
 </script>

And here is my input type text:

<input type="text" id="primerApellido" name="primerApellido" value="<?php echo $primerApellidoForm?>"/>

So as you can see, I need to save what is typed on the input on the PHP variable as soon as the user leave the text box. Right now it save literally the Javascript variable assignation, not the value that I need.

How can I assing the typed value on the text box to my PHP variable?

Vito
  • 718
  • 4
  • 16
  • 37

5 Answers5

3

I think you may be confusing PHP and Javascript a bit. PHP only runs on the server. Javascript only runs on the browsers.

You cannot modify PHP variables from javascript directly.

What you can do however, is make an ajax call to another page to do something with that variable such as save it in the session or into a database.

Check out the Javascript jQuery ajax command. With this command you can run another page without leaving the one you are currently on. You can have that page be a PHP page that receives the variable and performs an action with it.

Mark Carpenter
  • 433
  • 3
  • 9
  • Thank you, do you have any idea on how to implement what I need using Jquery Ajax? if possible, please put some code. – Vito May 23 '14 at 04:06
  • 1
    Check the first answer [here](http://stackoverflow.com/questions/370359/passing-parameters-to-a-jquery-function) .. That will show you how to use the ajax function. Then you write your php page that you point to to take that variable ( $_POST["variableName"] ) and do something with it. – Mark Carpenter May 23 '14 at 04:23
  • Thanks Mark, I followed your suggestion and answered the question with the code that make what I need. – Vito May 25 '14 at 20:58
0

Try this..

$("#primerApellido").onkeyup(function() {
    var user_input = $(this).val();
    $.ajax({
        type:'GET', 
        url: 'path/to/your/phppage',
        data:{user_input:user_input}, 
        success: function(response) {
            //alert(response);
        }
    });
});
user123456789
  • 556
  • 3
  • 12
  • 31
  • May i know the reason for down voting? – user123456789 May 23 '14 at 04:34
  • It's not an answer. It just shows how to send a variable to a page using AJAX. It doesn't show what to do with it (which is actually what the user is asking), and it doesn't show how to actually get the desired input from the form. Plus, you don't even make mention of the fact that you're using jQuery. Finally, there are a million examples of AJAX on SO. If you think this answer you've provided is sufficient, then instead mark the question as a duplicate and provide one of the million AJAX examples already on SO. – Cully May 23 '14 at 04:42
  • 1
    @CullyLarson we are here to give just an idea not full answers. – user123456789 May 23 '14 at 04:45
0

I followed the advice from Mark Carpenter to use Ajax function, below is the code that do what I want:

$("document").ready( function() 
$("#primerApellido").blur(function() {
    $.ajax({
    type: "GET",
        url: "destinationURL.php",
        data: "primerApellidoForm="+$('#primerApellido').val(),
        success: function(msg){
            alert( "Data Saved: "+msg);
         }
      });

   });
});

Now the PHP variable "primerApellidoForm" have the value from $('#primerApellido').val() input type.

Vito
  • 718
  • 4
  • 16
  • 37
-1

Not sure what you are trying to do is possible. Once you have loaded the page, there is no way you can run the php code ( You can make ajax requests though ) . Hope the following helps.

How can I use a JavaScript variable as a PHP variable?

Community
  • 1
  • 1
Shaggy
  • 193
  • 1
  • 8
  • Thanks, other persons suggested the Ajax option, please post a code example on how would you solve this – Vito May 23 '14 at 04:07
  • This is not a solution to the issue. It would be better to give such suggestions in the comments, rather than as an answer. – Cully May 23 '14 at 04:29
-1

you can try this...

<script type="text/javascript">

$("#primerApellido").blur(function() {
<?php $primerApellidoForm  = "<script>document.write($(this).val());</script>"?> 
});  
</script>
Bijay Rai
  • 961
  • 1
  • 12
  • 32
  • Are you trying to run Javascript from inside a PHP script? – Cully May 23 '14 at 04:29
  • PHP cannot execute Javscript (well, apart from special libraries). Javascript is executed in the client's browser, not on the server in a PHP script. – Cully May 23 '14 at 04:38
  • How is it client-side? It's never output. It's just assigned to a PHP variable. – Cully May 23 '14 at 04:43
  • you can call that php variable where ever you want in this page.... you can try echoing it.. did uh test it??? – Bijay Rai May 23 '14 at 04:44
  • The user is asking how to get a value from a form into a PHP variable. Your answer doesn't do that. You've just assigned some HTML Javascript code to a PHP variable. How does this retrieve a form value? – Cully May 23 '14 at 04:46
  • $(this).val() It returns the value of the element.. Please read the code first before commenting ... presence of mind is must buddy – Bijay Rai May 23 '14 at 04:49
  • You can't execute Javascript inside a PHP script. That's my point. Try writing something like this yourself. See if it works. – Cully May 23 '14 at 04:50
  • instead of giving us that suggestion why dont you give a try .. you are just entertaining me – Bijay Rai May 23 '14 at 04:55