Assistance with opening a URL in a new window.

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
jonahz
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Mar 25, 2017 1:37 pm

Sat Mar 25, 2017 2:03 pm

Using CURL to fetch a JSON response and populate a drop down list using "name". Response contains "name", "description", and "id" (Sample below). This "name" represents a user friendly application name. What I am trying to accomplish is when a user selects the "name" from the drop down and click a button (ex. "Submit"), it populates a URL with the corresponding "id" and then opens that URL in a new tab or window.

Example URL: http://myapplication.domain.com/locatio ... tion=[b]20[/b]

Sample response:
[
{
"name": "App1",
"description": "",
"id": 10
},
{
"name": "App2",
"description": "",
"id": 20
},
{
"name": "App3",
"description": "",
"id": 30
}
]
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 903
Joined: Mon Feb 22, 2016 5:52 pm

Sun Mar 26, 2017 7:23 am

Where's the rest of your script?
jonahz
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Mar 25, 2017 1:37 pm

Sun Mar 26, 2017 5:43 pm

That's just it I don't really have this part of the script. What I have so far is a CURL to grab the data, grab the "name" (application name) and put into an HTML drop-down (<option>). Ideally the user would select the application name from the drop-down and click a button (ex. submit) and the action would be to go to a URL. The rub is in the original JSON response i have the ID. I can only build the URL with the ID to launch into the UI and for that "application".

My CURL:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "restuser:password");
curl_setopt($ch, CURLOPT_URL, "http://myexample.domain.com/rest/applic ... utput=json");

$output_json = curl_exec($ch);

$output_array = json_decode($output_json, true);

curl_close($ch);

echo '<select>';

foreach($output_array as $loop_app {
echo "<option value"."</option>";
echo "$appName = $loop_app['name'];
}
echo '</select>';
?>

Ideally that would be encapsulated in an HTML <form> with the action somehow grabbing the "ID" and populating the URL and then opening it.

In the example the drop-down would show App1, App2, App3. Say they selected App2, the URL would be populated as shown w/the ID of 20 and then opening it.

Re-posting the example data:

Example URL: http://myapplication.domain.com/locatio ... ication=20

Sample response:
[
{
"name": "App1",
"description": "",
"id": 10
},
{
"name": "App2",
"description": "",
"id": 20
},
{
"name": "App3",
"description": "",
"id": 30
}
]
AdoptiveSolution
php-forum Super User
php-forum Super User
Posts: 167
Joined: Wed Jun 15, 2016 8:35 am

Sun Mar 26, 2017 11:13 pm

Curl and JSON has nothing to do with your problem.

What you need is a FORM with a SELECT menu and a GET method.

Standard HTML which you can find in the manual.

Code: Select all

<?php
$output_json = '[
	{
		"name": "App1",
		"description": "",
		"id": 10
	},
	{
		"name": "App2",
		"description": "",
		"id": 20
	},
	{
		"name": "App3",
		"description": "",
		"id": 30
	}
]';
$output_array = json_decode($output_json, true);
echo '<pre>';
print_r( $output_array );
echo '</pre>';
?>

<form action="receive.php" target="_blank" method="GET">
	<select name="location">
<?php
	foreach( $output_array as $loop_app ) {
		echo '<option value="' . $loop_app['id'] . '">' . $loop_app['name'] . '</option>';
	}
?>
	</select>
	<input type="submit" name="submit" value="submit" />
</form>
The recieve.php will process the info :

Code: Select all

<?php
if ( isset( $_GET['submit'] ) && isset( $_GET['location'] ) ) {
	echo $_GET['location'];
}
?>
jonahz
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Mar 25, 2017 1:37 pm

Mon Mar 27, 2017 9:04 am

Thank you for your reply and I think we are getting on the right track. When what you provided is executed it opens as http://localhost/recieve.php?location=10&submit=submit and prints the ID. However, the URL needs to open as shown;which is a completely different URL with the ID populating the number following application=.

http://myapplication.domain.com/locatio ... ication=20
AdoptiveSolution
php-forum Super User
php-forum Super User
Posts: 167
Joined: Wed Jun 15, 2016 8:35 am

Mon Mar 27, 2017 12:26 pm

Your problem was creating the SELECT dropdown list in a FORM.

You now have the solution to open a new browser window when you press SUBMIT.

Use a domainname URL instead of localhost.
Post Reply