4

I need to load a file inside a container, but using an argument - to get some data from database firstly:

$('#story').load('test.php');

test.php

$st = $db->query("select * from users where id = " . $id);  

... processing variables... load the finished content

Here I need $id from client side. Is it possible?

5
  • Possible duplicate of GET URL parameter in PHP Commented Feb 21, 2019 at 8:16
  • note that you should be very cautious not injecting directly the variable in your SQL string. bobby tables -> you should use prepared statements, and maybe do some checks first Commented Feb 21, 2019 at 8:18
  • @Kaddath sql injection on select statement? Commented Feb 21, 2019 at 8:19
  • yes, that can be used to get informations about other tables of your DB, using join and such, good habit to take anyway Commented Feb 21, 2019 at 8:23
  • @Kaddath, thanks, I believed select statement is safe Commented Feb 21, 2019 at 8:25

3 Answers 3

5

yes ..you could pass with url query

$('#story').load('test.php?id=1');

test.php

$id = isset($_REQUEST['id'])?$_REQUEST['id']):'';
$st = $db->query("select * from users where id = " . $id);  
Sign up to request clarification or add additional context in comments.

2 Comments

You can use null coalesce operator instead ternery operator as $id = $_REQUEST['id] ?? '';
@RohitGhotkar you are right.But its only supported on php 7 and above.Not with lower version stackoverflow.com/questions/34571330/…
0

You can use ajax request and on success you load your file something like:

                    $.ajax({
                        type: 'POST',
                        url: "test.php", //make sure you put the right path there
                        data: {id: id},
                        dataType: "json",
                        success: function (resultData) {
                            $('#story').load('test.php');
                        }
                    })

Just make sure that your php function returns/echos the id you want.

That way you make a call to your php file and when it's successful you will load your file and you can put extra logic there if you want to return more data to use it of course.

resultData holds the output of your php function so it's up to you what info you want to be there.

Comments

0

You could use Ajax to post the ID to your php code.

$.ajax({
type: "POST",
url: "test.php",
data: { 'id': foo },
cache: false,
success: function(){
   alert("Order Submitted");
  }
});

php:

<?php
$id = $_POST['id']; 

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.