0

I want to retrieve textbox value and assign it to php variable but without using form. I am doing search functionality in which i want to hide some contents on button click and and on the same click want searching function but if i use form then either it will redirect to another page or again reload the page which i don't want.

So i want some another method for this.

6
  • You tagged your post "ajax". So you know what's the instrument you're going to use, right? You didn't come here to learn how to use AJAX, did you? Commented Jun 1, 2012 at 9:06
  • read about javascript and ajax Commented Jun 1, 2012 at 9:06
  • @maxart :- I have read somewhere that it is possible with ajax also so that's why i add ajax as a tag and i don't come here to learn anything just asking for an idea or what approach should i use. So if u know any idea then tell me i don't ask for whole coding or want step by step guidence Commented Jun 1, 2012 at 9:11
  • Let me know if there is anything I'm missing, or aspect where you want more information, so I can improve my answer. Commented Jun 1, 2012 at 9:43
  • is there no method like var = textbox1.text. Only retrieving value in php is so hard. Commented Jun 1, 2012 at 9:49

4 Answers 4

2

Ajax, in addition of being a hero of the trojan war according to greek mythology... is the technology you need, and also your friend.

I recommend to use jQuery [As Barry has posted faster than me, because I wanted to make sure I got the mythology reference right...]

And as MaxArt has pointed out, there are actually two greek heroes named "Ajax", both did participate in the trojan war (and both in Homer's Iliad).


You can use jQuery like so:

<!-- Link jQuery (replace src with the location where you have jQuery) -->

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js">
</script>

<!-- Try something like this [having jQuery linked]: -->

<script type="text/javascript">
     function doButtonStuff()
     {
         //Use jQuery to "hide some contents"
         $('#stufftohideId').hide(); //stufftohideId identifies what to hide

         //set the url of the web page that will take this info on your server
         var ulr = "http://localhost";

         //The following part will fail if you cannot connect to the server
         $.post(
             url,
             {text: $('#textboxId').val()}, //textboxId identifies the textbox
             function (data)
             {
                 //data is what the server responded
                 //do something with data, for instance:
                 alert(data);
             }
         );
     }
</script>


<input type="text" id="textboxId"></input>
<input type="button" onclick="doButtonStuff();" value="Click Me"></input>

<div id="stufftohideId">
    <p>
        This is an example of something to hide.
    </p>
</div>

You will get on the server side, of the web page with the url you passed like so:

<?php $text = $_POST['text']; ?>

Make sure to verify the request method, for example:

<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $text = $_POST['text'];
        //do something with $text, for instance:
        echo $text;
    }
?>

And also to validate and sanitize the input. If a session is required verify that too. Whatever the server returns, will be recieved in the client side.

Sign up to request clarification or add additional context in comments.

7 Comments

Ajax is also a bathroom cleaning agent.
@ltiong_sh I'm aware of the cleaning uses of Ajax, but I didn't think that it was popular there in 123 Sesame Street.
Ajax is also a dutch football team... named after the cleaning agent, which was named after the Greek hero. (And actually there are two Greek heroes named Ajax...)
Thank you for your reply. But not working error is coming undefined index test. And no stuff is hiding.
@nitu_raj I've updated my answer steadly, it is subtle... but I did fix that one already. Please try the updated one.
|
1

If you don't want a page reload have a look at AJAX with for example jQuery: http://api.jquery.com/jQuery.ajax/

Comments

1

You can prevent page reload or redirect by using ajax implementation for PHP. You could check CakePHP, tppAjax...

Comments

1
$(document).ready(function() {

    $("#textboxID").change(function(){
        var text = $(this).val();
    });

});

Using jQuery, you can use the .change method within the document ready function to get the text field value. Once you get the value via jquery you can perform some ajax and send the javascript variable to a php page where you can use the $_GET method to get it and at this point you have your variable in the php and you can do whatever you want with it.

1 Comment

Thank you for your reply. But not working error is coming undefined index test.

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.