Posting a variable returned from a for-each loop

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

In this line, your createJob() function must be in JavaScript, I think, so you can probably drop the onSubmit() tag. I don’t think it’s even valid on an input, only on a form.

Later in your PHP you do call your createJob() PHP function based on a form variable being set, but your <form> tags for that exclude the information that you are displaying, and exclude your hidden input tag. A form submit only includes the inputs between the open and close form tags. I would think at least you’d need to move your opening form tag to before your first input tag for each client in the loop.

<tbody>
        <tr>
          // opening form tag probably wants to be here
          <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>
   

I can’t see any specific issue with working in this way - although the data in your client list is dynamic, it’s not dynamic in the sense that it alters once the page has been displayed.

1 Like

I’m now getting an output that says Array ( [Exception] => Array ( [Message] => A parameter cannot be found that matches parameter name 'ClientID'. when the onSubmit= is called and with about 9 more lines to the exception.

I don’t have any knowledge of javascript so this may have me stumped here. What would I need to do for it?

You don’t have any JavaScript in the code you posted - is there some JavaScript somewhere? If there is, please post that as well. Exactly where is that message coming from?

In PHP, the lines between server and client vanish… But in not such an awesome way.

PHP is serverside, Javascript is clientside.
And if you attach onSubmit, onClick or such handlers, the DOM tries to call the associated javascript function - which is clientside.

1 Like

My guess is that when you call the “new order” script on the external URL, it’s complaining about not finding a client ID. Is it the return from that call in createJob() that is giving the message? Have you moved the opening form tag to include your hidden form variable? I guess so, as your function would just exit if you hadn’t. Is the value correct for that variable?

By the way, that’s not happening because of the onSubmit() parameter, it’s happening because your PHP reloads, sees the $_POST['createJob_submit] variable exists, and calls your PHP code.

There is no javascript in my code.

Basically a run down of the program:

Someone searches for a client which sends an API request with the variable from php attached to the API.

The API returns data in JSON format which I then loop through and output in the table.

Once the data is outputted into the table a button appears allowing you to select that specific client.

That button then send another API request and adds the ID onto the end as a parameter and the API will execute.

I know that the function is now hitting because it hits the echo "job created successfully"; section of the function.

I hope this helps your understanding. If you want, I can post the entirety of this page as I only posted the functions.

EDIT:
The message is coming from this line in my createJob.php

print_r($job)

I also know from outputting $url that the ID is being appended onto the end so it may be an issue with the API itself now.

I fixed it.

In the createJob.php section I had this:

'url/?command=set%20new-WHMCSorder%20-ClientID%20'.$id

When I should have had this

'url/?command=new-WHMCSorder%20-ClientID%20'.$id;

That’s what I get for copying previous code and not checking through it I suppose.

Sorry for the waste of time and thank you for your help!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.