title: Problem loading dynamic files with PHP/AJAX
body: <p>I have created a fairly secure landing page for our clients to download copies of their contracts.</p>
  
  <p>You hit retrieve.php, the EULA (jQuery Dialog) appears.  After you sign and accept, the jQuery dialog disappears.</p>
  
  <p>An AJAX call is sent to the server to get the hashed filename and a link is generated to view the PDF securely in your browser.</p>
  
  <p>The problem is, now the company wants me to add a JPG version of the contract.  The contract is either 2 or 3 pages, depending on what service they want to use with us.</p>
  
  <p>The problem I am having is how do I generate the correct number of links for the jpg versions?  Everything is done in AJAX, and I would prefer not to return the number of images from AJAX.</p>
  
  <p>The image names are (same hash as pdf)-.jpg</p>
  
  <p>Does anybody have a dynamic way to do this?</p>
  
  <p>I wrote a function that does this in PHP, but since I am running the link generation on the client side, it will not work.</p>
  
  <p>My question is:
  I you were me, would you return all 3 or 4 links (pdf + however many jpgs there are) back through JSON?<br>
  Would you pass the number of pages back in json and create the links dynamically?
  Do you have any better solutions?</p>
  
  <p>Some code:
  Here is the AJAX Call that gets sends the info to the server then creates the links:</p>
  
  <pre><code>$.ajax({  type: 'POST',
  url: 'getContractAJAX.php',
     data: { 
        'pin' : pin.val(),
        'name' : signature.val().toUpperCase(),
        'lead' : '&lt;?php echo $_GET['lead']; ?&gt;'
     }, /* end data */
     cache: false,
     success: function(contractId) {
        if (contractId['success'] == true) {
           contractExpireDate = new Date(contractId['contractExpDate']);
           today = new Date();
           // Log the signature.
           $.post('log.php', {
              signature: $("#electronicSignature").val().toUpperCase(),
          pin: $("#eSpin").val(),
              lead: '&lt;?php echo $_GET['lead']; ?&gt;',
              method: 'SIGNATURE'},
              function(log) {
              if(log['success'] == true) {        
  
              /* DOWNLOAD IMAGES LINKS */
              $("#downloadLinks").prepend("&lt;a href='#' onclick='log(\""+contractId['contract']+"\", \"DOWNLOAD\");'&gt;&lt;img src='img/btnDownloadPdf.png' alt='Downdload PDF' /&gt;&lt;br /&gt;Download Contract in Adobe &amp;copy; PDF&lt;/a&gt;");
  
           } else {
              alert("There was a problem! Please contact customer support.");
              showDisclosure();
           } /* end else */
                                              }); /* end function(log) */
                                              $("#dialog-confirm").dialog("close");
  </code></pre>
  
  <p>getContractAJAX.php:</p>
  
  <pre><code>if (isset($_POST['pin']) &amp;&amp; isset($_POST['name']) &amp;&amp; isset($_POST['lead'])){
      $pin = $_POST['pin'];
      $name = trim($_POST['name']);
      $email = $converter-&gt;decode($_POST['lead']);
  
  
  
      /*
       * 
       * 
       * PDO
       * 
       * 
       */
      $stmt = $dbh-&gt;prepare("SELECT contract, contractExpireDate 
                              FROM users 
                              WHERE   emailAddress=:email AND 
                                      pin=:pin AND 
                                      concat(firstName, ' ', lastName) LIKE :name AND
                                      contractExpireDate &gt; NOW()
                              LIMIT 1");
  
      if ($stmt-&gt;bindParam(':email', $email, PDO::PARAM_STR) &amp;&amp;
          $stmt-&gt;bindParam(':pin', $pin, PDO::PARAM_INT) &amp;&amp;
          $stmt-&gt;bindParam(':name', $name, PDO::PARAM_STR)) {
          $stmt-&gt;execute();
  
          $found = 0;
          foreach ($stmt-&gt;fetchAll(PDO::FETCH_ASSOC) as $el) {
              $found++;
              $contract = $el['contract'];
              $contractExpDate = $el['contractExpireDate'];
          }
          $stmt = null;
      }               
  
      if ($found &gt; 0) {
          $success = true;
      } else {
          $success = false;
          $error = "NO MATCHES FOUND &gt;&gt;&gt; $email &gt;&gt;&gt; $pin &gt;&gt;&gt; $name";
      }
  
  
  }else{
      $success = false;
      $error = "NOT ALL PASSED";
  }
  
  header('Content-type: application/json');
  $json = array("contract" =&gt; $contract,
                "contractExpDate" =&gt; $contractExpDate,
                "success" =&gt; $success,
                "messege" =&gt; $error);
  echo json_encode($json);
  </code></pre>