Jump to content

Dynamic form issue


scottybwoy

Recommended Posts

Hi All,

I have a bit of a complicated issue thats racking my brain.  Was wondering if someone could make sense of it?

I have a client page that can be accessed in a couple of ways.  Either from a customer page or from an admin page.  If it comes from the customer page it's ready to insert a new client if the customer does not have a client issued to it, it will not generate an ID and displays the wrong form layout (Select box for customer, when it already has that data).

Next problem is, if it comes from the admin button it follows a few clauses:  Like if it has some data it can move on to different stages.  with no data it shows a select menu of customers, on selection of a customer it should bring up a select menu for the available clients for that customer, however it just passes on to the next stage of trying to insert a new client, however without generating an clientID.  I want it to do that process when there are no clients for that particular company, but thats not the case.

Let me provide some code to put this in context :
[code]
<?php
function recieveClient()
{
global $clientId, $custId, $sqlC, $sqlCl, $template;

// Makes the select queries for template
$sqlC = mssql_query("SELECT custId, company FROM customers")
or die("Company Query Failed");
$sqlCl = mssql_query("SELECT clientId, name FROM client WHERE custId = '" . $custId . "'")
or die("Client Query Failed");

// no clientId? But making one is possible, do it!
if (empty($clientId) && !empty($custId))
{
global $clientId, $company;

echo "just goes for the first one";
$sql = "SELECT company FROM customers WHERE custId = '" . $custId . "'";
$company = mssql_result(mssql_query($sql), 0, 0);

// Don't make one if user may wanna select one themselves.
if ($_POST['select'] == 'Select Company')
{
if (!$row = mssql_fetch_row($sqlCl)) {
$clientId = $this->addClient($custId);
}

} else {
$clientId = $this->addClient($custId);
}

$template = CLIENT_PAGE;
return $clientId;
// User wants to add something
} elseif (isset($_POST['commit']) && $_POST['commit'] == "Commit") {
global $clientId;

//echo "If commit";
$clientId = $_POST['clientId'];

$sql = "SELECT * FROM client WHERE clientId = '" . $clientId . "'";
$qry = mssql_query($sql);

if ($row = mssql_fetch_assoc($qry))
{
//echo "<br>Update Client";
$data_array = $row;

$this->updateClient($data_array);
} else {
//echo "<br>Insert Client";
$data_array = $_POST;

$this->insertClient($data_array);
}

$template = CLIENT_PAGE;
} elseif (!empty($clientId) && !empty($custId)) {
global $clientId, $company;

echo "final elseif clause";
$sql = "SELECT company FROM customers WHERE custId = '" . $custId . "'";
$company = mssql_result(mssql_query($sql), 0, 0);

$template = CLIENT_PAGE;
} else {
//echo "else clause";
//$template = CLIENT_PAGE;
require(CLIENT_PAGE);
}

return $template;
}
?>
[/code]
Thats generated from my client script. The next part is from the template.
[code]
<?php
if (isset($clientId) && isset($custId)) {
?>
<form name="clientAdmin" action='home.php' method='POST'>
<div class='l'>
<label for='company' class='labelL'>Company :</label>
<input type='text' id='company' value='<?php echo $company ?>' name='company' size='25%' class='required labelL' readonly />
</div>
<div class='r'>
<label for='clientId' class='labelL'>Client ID :</label>
<input type='text' id='clientId' value='<?php echo $clientId ?>' name='clientId' size='25%' class='required labelL' readonly />
</div>
<?php
} elseif ($_REQUEST['select'] == 'Select Company') {
?>
<form name="clientSelect" action='home.php' method='POST'>
<div class='l'>
<label for='company' class='labelL'>Company :</label>
<input type='text' id='company' value='<?php echo $company ?>' name='company' width='25%' class='required labelL' readonly />
</div>
<div class='r'>
<label for='clientId' class='labelL'>Client ID :</label>
<?php
if (!$row = mssql_fetch_row($sqlCl)) {
?>
<input type='text' id='clientId' value='<?php echo $clientId ?>' name='clientId' width='25%' class='required labelL' readonly />
<?php
} else {
?>
<select action='submit' id='clientId' name='clientId' tabindex='1' width='25%'>
<?php
while ($clRow = mssql_fetch_array($sqlCl)) { // Loop through each element
print("<option value=\'" . $clRow[0] . "\'>" . $clRow[1] . "</option>");
}
?>
</select>
<input type='submit' name='select' id='select' value='Select Client' class='btn' onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" />
<?php
}
?>
</div>
<?php
} else {
?>
<form name="custSelect" action='home.php' method='POST'>
<div>
<label for='custId' class='labelL'>Company :</label>
<select action='submit' id='custId' name='custId' tabindex='1' width='25%'>
<?php
while ($cRow = mssql_fetch_array($sqlC)) { // Loop through each element
print("<option value=\'" . $cRow[0] . "\'>" . $cRow[1] . "</option>");
}
?>
</select>
<input type='submit' name='select' id='select' value='Select Company' class='btn' onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" />
</div>
<?php
}
?>
[/code]
Then the rest of the form.

So the data comming from the customer page consists of 2 variable $custId and $company this goes into the client script where it generates the $clientId and diplays the form correctly.  Same data comes if there are no clients to that customer, however it displays the form using the else clause above not the first if clause as expected.

For the second part most of the data comes from itself.  i.e. starts on the else clause and should work itself up to the if clause.  But it seems to miss the elseif clause out and display the if clause with no data!  So I hope that someone can spot where I've gone wrong as it is doing my head in, I am now so confused, like I'm stuck in a while (this error occurs) { loop } lol.

Thanks
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.