0

I need to show json data from dynamic url in my website. When visitor make input which is his loyalty card number that number is added to url as suffix. That url is url that contains his details related to loyalty program (points he earned so far etc.).

My code:

<form action="" method="get">
<input type="text" id="unos" name="card" value = ""><br><br>
<input type="submit" name="submit" value="Provjeri bodove">
</form> 
<?php
$card = $_GET["card"];
$url='SERVER_URL_HERE/cardcheck/testAlsLoyalty.php?card= <?php echo $card; ?> ';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->user;
echo $jo->card;
echo $jo->rezultat;
echo $jo->bodova;
echo $jo->raspolozivo;

?>

What I got so far is that this code add input as suffix to url of my website, and if I put url of json website as form action value visitor is taken to json website where he can see data. But I want to load data from that url at my website (html or php) after he type card number.

Thank you in advance

4
  • Not sure what "if I put url of json website as form action" means, what json website? Also no need to <?php echo in ?card= <?php echo $card; ?> ', just use ?card=' . $card; Commented Feb 22, 2022 at 11:14
  • On first page load $_GET["card"] will (probably) not be set, make sure it is set before you execute your PHP code Commented Feb 22, 2022 at 11:16
  • By "json website" I mean url of json. I want user to see data from that url on my website not to redirected to json url. Commented Feb 22, 2022 at 11:20
  • For a future reference, you should remove any public IP addresses and domains from your question, there's bad people even on SO and you wouldn't want them to spam your server with requests would you? Commented Feb 22, 2022 at 11:22

2 Answers 2

2

You should check that the variable you're trying to use later on is actually set and also before you append the card number to the url, use rawurlencode() before submitting, otherwise that could break the url (when someone uses unallowed URL characters - i.e. spaces).

<form action="" method="get">
<input type="text" id="unos" name="card" value = ""><br><br>
<input type="submit" name="submit" value="Provjeri bodove">
</form> 
<?php

if(isset($_GET["card"])){
    $card = $_GET["card"];
    $url='http://SERVER_URL_HERE/cardcheck/testAlsLoyalty.php?card=' . rawurlencode($card);
    $json = file_get_contents($url);
    $jo = json_decode($json);
    echo $jo->user;
    echo $jo->card;
    echo $jo->rezultat;
    echo $jo->bodova;
    echo $jo->raspolozivo;
}else{
    //Notify user here that no card number has been submitted
}

?>

Note that I removed the IP address and port from the URL and replaced with SERVER_URL_HERE. You should never ever publish your public IP address (or domain) as it just opens doors for attackers, especially when you directly give the endpoint...

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

3 Comments

jnko, I don`t get results with your code, and I think it is because of the way you wrote url.
@Miha did you do $_GET['card'] = rawurldecode($_GET['card']); on the testAlsLoyalty.php side?
Sorry jnko, your code is working, everything is ok. Thank you very much.
0

You don't use <?php echo in strings. Use string concatatenation.

$url='http://81.93.93.78:8085/cardcheck/testAlsLoyalty.php?card=' . $card;

<?php echo ... ?> is used when you've exited PHP code with ?>, and want to insert a PHP expression in the output.

3 Comments

I got: Notice: Undefined index: card after your url code Barmar
That shouldn't happen inside if(isset($_GET['card'])).
Sorry. My mistake. Now I got whta I want. Thank you Bramar!

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.