koool
koool

Reputation: 15517

passing a value to php from textbox using jquery doesnt work please help

<script type="text/javascript" src="http://code.jquery.com/jquery-1.5b1.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js"></script>  

<input type="text" id="textbox">
<button id="inlinesubmit_button" type="button">submit</submit>


<script>

function call_this()
{
alert(<?php echo $_POST['text'];?>);
}


$('#inlinesubmit_button').click(function(){
    $.ajax({
        type: "POST",
        url: "sync_loading1.php",
       data: {text:$('#textbox').val()}
    });

call_this();
});

</script>

this gives undefined in alert box please help me in pointing out what am I doing wrong

Upvotes: 0

Views: 3449

Answers (3)

John Green
John Green

Reputation: 13435

Hard to figure out what you're doing... but the key is that you need the client code, plus the server handler.

Your HTML:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.5b1.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js"></script>  
<script type="text/javascript">
function call_this(data, status, xhr)
{
   alert(data);
}
$(document).ready(function(){
   $('#inlinesubmit_button').click(function(){
      $.ajax({
         type: "POST",
          url: "sync_loading1.php",
         data: {text:$('#textbox').val()},
      success: call_this
      });
   });
 });
</script>

<input type="text" id="textbox">
<button id="inlinesubmit_button" type="button">submit</submit>

sync_loading1.php

<?php
// Perform action.  Maybe you want to log this into a DB.
//   Maybe you want to just print out some text:
echo nl2br('Hello world.  Here was my input:'.$_POST['text']);

Upvotes: 2

Ascherer
Ascherer

Reputation: 8083

This function isnt going to work sadly, you are running an ajax call to an external file, and the external file cant do an alert because its not being viewed by the user. if you want to alert the data of the text field, just do alert($('#textbox').val());

Upvotes: 1

alex
alex

Reputation: 490163

You probably need to wrap it in quotes (' or "), to make it a string.

alert('<?php echo $_POST['text'];?>');

It also appears you are overlooking something with how JavaScript runs (client side) and PHP (server side). They can't directly communicate; PHP can echo JavaScript, and JavaScript can make HTTP requests to PHP files.

Upvotes: 2

Related Questions