Jump to content

Extracting info from a database and adding checkboxes to an existing form.


teccyspice

Recommended Posts

Hi,

 

I've been stuck for ages trying to get a form working.

 

A mockup is here; http://www.nutworks.co.uk/allocate/multibook.php

 

The idea is for the user to enter the details, who, where and the date range then click on the Additional Systems checkboxes. This extracts the list of free systems from the database and displays these within the form using AJAX/Javascript.

 

 

I have tried to extract the value of the individual systems when checked as the php script puts these in a reference[] array but I cant get any values from the updated form, reference[] does not seem to exist.

 

I'm obviously missing something but my php skills are limited and my java/ajax are kinda limited to what I have done here.

 

=========================

allocate.js

/*

  Use AJAX functions to populate the system fieldsets with the output from the getsystems.php script

 

*/

 

var xmlHttp

 

// Get the system, start and end dates

function showSUN(system,frm_startdate,frm_enddate) {

  xmlHttp=GetXmlHttpObject()

 

  if (xmlHttp==null) {

    alert ("Browser does not support HTTP Request")

  return

  }

 

  // Execute getsystems.php and pass the system selected with the start and end dates from the form.

  var url="getsystems.php?system="+system+"&frm_startdate="+frm_startdate+"&frm_enddate="+frm_enddate

 

  // If the state is changed, changeSUN and pass the output from url.

  xmlHttp.onreadystatechange=changeSUN

  xmlHttp.open("GET",url,true)

  xmlHttp.send(null)

}

 

/*

  If the ready state = 4 (or complete) then get the Element id "txtSUN" and replace the element with the output of getsystems.php

 

  Repeat the change function for every platform type

*/

function changeSUN() {

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

  document.getElementById("txtSUN").innerHTML=xmlHttp.responseText

  }

}

 

=========================================

getsystems.php

 

<?php

 

error_reporting(E_ALL);

 

 

// START THE OUTPUT BUFFER

ob_start();

 

// GET THE DATA

echo "\nGET: ";

var_dump($_GET);

echo "\nPOST: ";

var_dump($_POST);

 

 

$system = '';

$frm_startdate = '';

$frm_enddate = '';

$system = $_GET["system"];

$frm_startdate = $_GET["frm_startdate"];

$frm_enddate = $_GET["frm_enddate"];

 

//echo "SYSTEM = " .  $system  . "<br>";

//echo "START_DATE = " .  $frm_startdate  . "<br>";

//echo "END_DATE = " .  $frm_enddate  . "<br>";

 

$con = mysql_connect($dbhost,$dbuser,$dbpass);

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

 

mysql_select_db($database, $con);

 

if ( $system == "TAPE"){

  $sql="SELECT DISTINCT a.location, a.hwtype, a.reference, a.make, a.model, a.sys_otherio, a.sys_tape FROM system_list a LEFT JOIN test_dates b ON b.referen

ce = a.reference WHERE a.location = 'LEI' AND a.reference LIKE '%" .  $system . "%' AND a.reference NOT IN ( SELECT DISTINCT l.reference FROM system_list l L

EFT JOIN test_dates d ON d.reference = l.reference WHERE d.reference IS NOT null AND ( d.start_date BETWEEN '" .  $frm_startdate . "' AND '" .  $frm_enddate

. "' OR d.end_date BETWEEN '" .  $frm_startdate  . "' AND '" .  $frm_enddate  . "' OR (d.start_date <= '" .  $frm_startdate  . "' AND d.end_date >= '" .  $f

rm_enddate  . "'))) GROUP BY a.reference ORDER BY a.sys_tape";

//echo $sql;

} else {

  $sql="SELECT DISTINCT a.location, a.hwtype, a.reference, a.make, a.model, a.sys_numcpu, a.sys_cputype, a.sys_memory, a.sys_totaldisk, a.sys_disk, a.sys_cd

dvd, a.sys_network, a.sys_otherio, a.sys_tape FROM system_list a LEFT JOIN test_dates b ON b.reference = a.reference WHERE a.reference LIKE 'QS-" .  $system

. "%' AND a.reference NOT IN ( SELECT DISTINCT l.reference FROM system_list l LEFT JOIN test_dates d ON d.reference = l.reference WHERE d.reference IS NOT nu

ll AND ( d.start_date BETWEEN '" .  $frm_startdate . "' AND '" .  $frm_enddate  . "' OR d.end_date BETWEEN '" .  $frm_startdate  . "' AND '" .  $frm_enddate

. "' OR (d.start_date <= '" .  $frm_startdate  . "' AND d.end_date >= '" .  $frm_enddate  . "'))) GROUP BY a.reference";

//echo $sql;

}

 

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))

{

  if ( $system == "SUN" ){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "

MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy

s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "HP"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "

MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy

s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "RS"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "

MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy

s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "INTEL"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "

MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy

s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "AS400"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "

MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy

s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "DISK"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_o

therio'] . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "TAPE"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['sys_tape']  . " - " . $row['sys_otherio'] . "</small><br>";

  }

  if ( $system == "MTS"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

  if ( $system == "CON"){

      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";

      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";

  }

 

  echo "<div class='reference'><input type='checkbox' name='" . $row['reference']  . "' id='" . $row['reference']  . "' /><a onmouseover=\"Tip('" . $systemt

ip . "',TITLE,'SYSTEM INFORMATION',TITLEALIGN,'center',SHADOW,'true')\" onmouseout=\"UnTip()\">" . $systemref . "</a></div>";

}

 

mysql_close($con);

?>

 

 

Link to comment
Share on other sites

sorry about that... I'll preview next time.

 

allocate.js

/*
   Use AJAX functions to populate the system fieldsets with the output from the getsystems.php script

*/

var xmlHttp

// Get the system, start and end dates
function showSUN(system,frm_startdate,frm_enddate) {
  xmlHttp=GetXmlHttpObject()

  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
  return
  }

   // Execute getsystems.php and pass the system selected with the start and end dates from the form.
   var url="getsystems.php?system="+system+"&frm_startdate="+frm_startdate+"&frm_enddate="+frm_enddate

  // If the state is changed, changeSUN and pass the output from url.
  xmlHttp.onreadystatechange=changeSUN
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}

/*
   If the ready state = 4 (or complete) then get the Element id "txtSUN" and replace the element with the output of getsystems.php

   Repeat the change function for every platform type
*/
function changeSUN() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
   document.getElementById("txtSUN").innerHTML=xmlHttp.responseText
   }
}

 

getsystems.php

<?php

error_reporting(E_ALL);


// START THE OUTPUT BUFFER
ob_start();

// GET THE DATA
echo "\nGET: ";
var_dump($_GET);
echo "\nPOST: ";
var_dump($_POST);


$system = '';
$frm_startdate = '';
$frm_enddate = '';
$system = $_GET["system"];
$frm_startdate = $_GET["frm_startdate"];
$frm_enddate = $_GET["frm_enddate"];

//echo "SYSTEM = " .  $system  . "<br>";
//echo "START_DATE = " .  $frm_startdate  . "<br>";
//echo "END_DATE = " .  $frm_enddate  . "<br>";

$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db($database, $con);

if ( $system == "TAPE"){
   $sql="SELECT DISTINCT a.location, a.hwtype, a.reference, a.make, a.model, a.sys_otherio, a.sys_tape FROM system_list a LEFT JOIN test_dates b ON b.referen
ce = a.reference WHERE a.location = 'LEI' AND a.reference LIKE '%" .  $system . "%' AND a.reference NOT IN ( SELECT DISTINCT l.reference FROM system_list l L
EFT JOIN test_dates d ON d.reference = l.reference WHERE d.reference IS NOT null AND ( d.start_date BETWEEN '" .  $frm_startdate . "' AND '" .  $frm_enddate
. "' OR d.end_date BETWEEN '" .  $frm_startdate  . "' AND '" .  $frm_enddate  . "' OR (d.start_date <= '" .  $frm_startdate  . "' AND d.end_date >= '" .  $f
rm_enddate  . "'))) GROUP BY a.reference ORDER BY a.sys_tape";
//echo $sql;
} else {
   $sql="SELECT DISTINCT a.location, a.hwtype, a.reference, a.make, a.model, a.sys_numcpu, a.sys_cputype, a.sys_memory, a.sys_totaldisk, a.sys_disk, a.sys_cd
dvd, a.sys_network, a.sys_otherio, a.sys_tape FROM system_list a LEFT JOIN test_dates b ON b.reference = a.reference WHERE a.reference LIKE 'QS-" .  $system
. "%' AND a.reference NOT IN ( SELECT DISTINCT l.reference FROM system_list l LEFT JOIN test_dates d ON d.reference = l.reference WHERE d.reference IS NOT nu
ll AND ( d.start_date BETWEEN '" .  $frm_startdate . "' AND '" .  $frm_enddate  . "' OR d.end_date BETWEEN '" .  $frm_startdate  . "' AND '" .  $frm_enddate
. "' OR (d.start_date <= '" .  $frm_startdate  . "' AND d.end_date >= '" .  $frm_enddate  . "'))) GROUP BY a.reference";
//echo $sql;
}

$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
   if ( $system == "SUN" ){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "
MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy
s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "HP"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "
MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy
s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "RS"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "
MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy
s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "INTEL"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "
MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy
s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "AS400"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_numcpu'] . " x " . $row['sys_cputype'] . "<br>" . $row['sys_memory'] . "
MB Memory<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_cddvd'] . "<br>" . $row['sys_network'] . "<br>" . $row['sy
s_otherio'] . "<br>" . $row['sys_tape'] . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "DISK"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>" . $row['sys_totaldisk'] . " GB Disk (" . $row['sys_disk'] . "GB )<br>" . $row['sys_o
therio'] . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "TAPE"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['sys_tape']  . " - " . $row['sys_otherio'] . "</small><br>";
   }
   if ( $system == "MTS"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }
   if ( $system == "CON"){
      $systemtip = $row['reference']  . "<br>" . $row['model']  . "<br>";
      $systemref = $row['reference']  . "<br>      <small>" . $row['model']  . "</small><br>";
   }

   echo "<div class='reference'><input type='checkbox' name='" . $row['reference']  . "' id='" . $row['reference']  . "' /><a onmouseover=\"Tip('" . $systemt
ip . "',TITLE,'SYSTEM INFORMATION',TITLEALIGN,'center',SHADOW,'true')\" onmouseout=\"UnTip()\">" . $systemref . "</a></div>";
}

mysql_close($con);
?>

 

Link to comment
Share on other sites

:-[ Oh dear, it looks like I made sooo many changed I trashed that bit...

 

It's in the getsystems.php right at the bottom and it now looks like this;

echo "<div class='reference'><input type='checkbox' name='reference[]' id='reference[]' /><a onmouseover=\"Tip('" . $systemtip . "',TITLE,'SYSTEM INFORMATION',TITLEALIGN,'center',SHADOW,'true')\" onmouseout=\"UnTip()\">" . $systemref . "</a></div>";
[code]





Link to comment
Share on other sites

Okay so in your JavaScript function:

function showSUN(system,frm_startdate,frm_enddate) {
  xmlHttp=GetXmlHttpObject()

  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
  return
  }

   // Execute getsystems.php and pass the system selected with the start and end dates from the form.
   var url="getsystems.php?system="+system+"&frm_startdate="+frm_startdate+"&frm_enddate="+frm_enddate

  // If the state is changed, changeSUN and pass the output from url.
  xmlHttp.onreadystatechange=changeSUN
  xmlHttp.open("GET",url,true)
  xmlHttp.send(null)
}

 

It doesn't pass the reference parameter to the AJAX url. I don't know where you called that function, but perhaps you should add reference to the AJAX URL.

Link to comment
Share on other sites

It doesn't pass the reference parameter to the AJAX url. I don't know where you called that function, but perhaps you should add reference to the AJAX URL.

 

The code from the multibooking.php script is here.

<fieldset>
   <legend>Additional Systems</legend>
   <div class="systems"><input type="checkbox" name="SUN" id="SUN" value="SUN" onclick="showSUN(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value); " /><label for="multi_systems">SUN</label>
  </div>
   <div class="systems"><input type="checkbox" name="RS" id="RS" value="RS" onclick="showRS(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value); " /><label for="RS">RS6000</label></div>
   <div class="systems"><input type="checkbox" name="HP" id="HP" value="HP" onclick="showHP(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="HP">HP9000</label></div>
   <div class="systems"><input type="checkbox" name="INTEL" id="INTEL" value="INTEL" onclick="showINTEL(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="INTEL">INTEL</label></div>
   <div class="systems"><input type="checkbox" name="AS400" id="AS400" value="AS400" onclick="showAS400(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="AS400">AS400</label></div>
   <br />
   <div class="systems"><input type="checkbox" name="DISK" id="DISK" value="DISK" onclick="showDISK(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="DISK">DISK</label></div>
   <div class="systems"><input type="checkbox" name="TAPE" id="TAPE" value="TAPE" onclick="showTAPE(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="TAPE">TAPE</label></div>
   <div class="systems"><input type="checkbox" name="MTS" id="MTS" value="MTS" onclick="showMTS(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="CYCLADES">CYCLADES</label></div>
   <div class="systems"><input type="checkbox" name="CON" id="CON" value="CON" onclick="showCON(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="CON">CONSOLE</label></div>
   <div class="systems"><input type="checkbox" name="MOBILE" id="MOBILE" value="MOBILE" onclick="showMOB(this.value,this.form.frm_startdate.value,this.form.frm_enddate.value)" /><label for="MOBILE">MOBILES</label></div>
</fieldset>

<br />
<fieldset class="results">
   <legend>SUN</legend>
   <div id="txtSUN">
      <div align="center">Item has not been selected.</div>
   </div>
</fieldset>

 

When the user clicks on the SUN checkbox, it is sent to getsystems.php with the start and end dates, this does the search within the date range and posts the results back to the txtSUN div... bear in mind that I only did AJAX and Javascript for this I whilst I did manage to get it working (of sorts) I'm not by any stretch of the imagination, an expert...

 

When you say I need to add reference to the ajax url, I dont really know what you mean...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.