Hi there,
I’ve currently got some code which returns some data in JSON format and outputs it to the screen in a table. This code is run from one function. After this, a table will appear with the data displayed and a button next to each name saying ‘Select’.
What I want to happen is, when I press select, it runs another function which sends another API call with the variable specified.
The issue is, I can’t seem to get it to work correctly. I’ve searched the internet and i’m reading that you can’t use the isset()
function with results from a foreach loop to do a check.
The data gets returned and put into a label in the table, so i’ve put a hidden field with the same data so that gets sent in $_POST
but i’ve also read that that is bad for dynamic information (which this will be)
Here’s the code, I could appreciate if any of you could have a look.
veeam.php (main page)
<?php
function invokeapi()
{
// set variables
$client = $_POST["clientsearch"];
$username = 'usernamet';
$password = "password";
$url = 'url/?command=Check-ClientWHMCS%20'.$client;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl options
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$output = curl_exec($ch);
curl_close($ch);
$clientlist = json_decode($output, 1); //decode curl result
#turns object into array if singular
if (isset($clientlist['id'])) {
$clientlist = [$clientlist];
}
if($clientlist != ""){
?>
<table>
<caption>Clients Found</caption>
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Forename</th>
<th scope="col">Surname</th>
<th scope="col">Email</th>
<th scope="col"></th>
</tr>
</thead>
<tr>
<?php
//loops through result and displays in table
foreach($clientlist as $clients){
?>
<tbody>
<tr>
<td data-label="ID"><?php echo $clients['id'];?></td>
<input type="hidden" name="lblClientID" value="<?php echo $clients['id']; ?>">
<td data-label="Forename"><?php echo $clients['firstname'];?></td>
<td data-label="Surname"><?php echo $clients['lastname'];?></td>
<td data-label="Email"><?php echo $clients['email'];?></td>
<td>
<form method="post" action= "veeam.php">
<input type ="submit" name="createJob_submit" value="select" onSubmit="createJob()"/>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<table>
<td data-label="ID"></td>
<form method="post" action= "veeam.php">
<td data-label="name"><input type="text" name="name" class="field-divided" placeholder="Enter first name" /></td>
<td data-label="lastname"><input type="text" name="lastname" class="field-divided" placeholder="Enter last name" /></td>
<td data-label="email"><input type="email" name="email" class="field-divided" placeholder="Enter email address" /></td>
<td>
<input type ="submit" name="newclient_submit" value="create" onSubmit="newClient()"/>
</td>
</form>
</table>
<?php
}
else{
echo "Person not found";
?>
<table>
<td data-label="ID"></td>
<form method="post" action= "veeam.php">
<td data-label="name"><input type="text" name="name" class="field-divided" placeholder="Enter first name" /></td>
<td data-label="lastname"><input type="text" name="lastname" class="field-divided" placeholder="Enter last name" /></td>
<td data-label="email"><input type="email" name="email" class="field-divided" placeholder="Enter email address" /></td>
<td>
<input type ="submit" name="newclient_submit" value="create" onSubmit="newClient()"/>
</td>
</form>
</table>
<?php
}
}
if(isset($_POST['clientsearch']))
{
invokeapi();
}
if(isset($_POST['newclient_submit']))
{
newClient();
}
if(isset($_POST['createJob_submit']))
{
createJob();
}
?>
And here’s the code for the function which isn’t working correctly. createJob.php
<?php
function createJob(){
if (isset($_POST['lblClientID']))
{
$id = $_POST['lblClientID']; # variable set
echo $_POST['lblClientID'];
$username = 'username';
$password = "password";
$url = 'url/?command=set%20new-WHMCSorder%20-ClientID%20'.$id; # append API URL with variables
$ch = curl_init(); # initiate curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); # all needed for curl command
$output = curl_exec($ch);# stores output into variable
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch); # echo error if curl failed
}
curl_close($ch);
$job = json_decode($output, 1); #decodes output into readable json array
//print_r($job); #echos out json
echo "job created successfully";
}
else{
echo "Please fill in the fields";
}
}
?>
Any pointers would be beneficial because this is probably the most complex project i’ve had to do in PHP to date.
Thank you