0

i want to pass a jquery variable into php, l'ets say: i have this input:

<input type="text" name="" id="course" />

an jquery:

$.get('test.php',
 function(data) {
 $('#details').html(data);
  }
 );

and test.php:

$sql = "select name, id, parent_id from mytable where parent_id='theinput'";

so basiclly i want the input data that filled by user to be in the php query ('theinput')

Redwan Nassim
  • 367
  • 3
  • 7
  • Please don't do this yourself. Passing through unprocessed user input like this is dangerous not just to your website, but any poor schmuck who might be stuck on your server. Start with safely using user input http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php, using jquery to submit information asynchronously is the final step of a long journey. – Sinetheta Jan 19 '12 at 23:04
  • One thing I noticed is that you want the $.get to be inside the submit event. Additionally you want it to pass the input so modify your $.get to add another parameter so $.get('test.php', "parent_id=$('#course').val", function(data) {... etc. – jakx Jan 19 '12 at 23:06
  • No offense meant at all, but you seriously need to do some more research before getting into anything even this complex. It's not really hard, but as Sin mentioned, what you're showing in code is extremely data hackable and horrible practice. If I may make a suggestion, jQuery is not your only problem, maybe do some research on something like Codeigniter, which can help encrypt your data as well as provide a useful class they call [Active Record](http://codeigniter.com/user_guide/database/active_record.html). It does alot of the hard work for you. After you do some more research, come back :) – SpYk3HH Jan 20 '12 at 03:49
  • FYI Codeigniter is for PHP and from what i see of your sql statement, it would do you some good to learn how to use their active class – SpYk3HH Jan 20 '12 at 03:50

2 Answers2

0
<input type="text" name="course" id="course" />
<script>
  var data = $('#course').val();
  $.get('test.php?data='+data);
<script>

Make the the name of the textbox that same as the id.

Take the value of the textbox with a variable which takes data from the elements val.

You could write it like this:

$('#course').focusout(
    function ()
    {
      var data= $(this).attr('value');

       load('test.php?data='+data);
    }
);
sth
  • 222,467
  • 53
  • 283
  • 367
iMoldovean.com
  • 141
  • 1
  • 1
  • 6
0

use this:

$.get("test.php",
          {"param":"param value"},
          function(returned_data)
              {
              alert(returned_data);
              });

in the text.php get the value of "param" and write the mysql query.

progrrammer
  • 4,475
  • 2
  • 30
  • 38