Jump to content

if else not allowing me to change


zimmo

Recommended Posts

I have a form which allows someone to fill in and then come back to make changes should they need to. The problem I have is with the drop down. If you initially select the agent option it does not require the vat number. All fine. Then when they come back to make a change, they need to change the account type to dealer, it will not update as it keeps defaulting back to agent. I am stuck as to what to do to fix it. I want it to allow that option but then require the vat number.

 

Here is my current code not including the connection etc.. as that is standard.

 

<?
if ( $_POST['submit'] ) {
$account_type = $_POST['account_type'];
$vat_number = $_POST['vat_number'];


if ( empty($account_type) ) {
  $error['account_type_error'] = '<div class="formerror">Please select your Account Type.</div>';
}
	if ( empty($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">In order for Pegbookers to act as a dealer on your behalf you must enter a correct VAT number.</div>';
		}
	elseif (!validVatnumberChars ($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">Please enter your VAT number as 123456789 (9 digits only).</div>';
	}
	elseif (!validVatnumberLength ($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">In order for Pegbookers to act as a dealer on your behalf you must enter a correct VAT number</div>';
	}
	elseif (!is_numeric($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">Please enter numbers only.</div>';
	}

$sql = "SELECT * FROM details WHERE fishery_id = '$_SESSION[fishery_id]' "; 
$sql_result = mysql_query($sql); 

if (mysql_num_rows($sql_result) ==0)
{
ob_start()
?>
<form name="details" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Details</legend>
<p>Please enter your details below, read the notes carefully.</p>
<p><label for="account_type">Account Type</label> 
<select name="account_type" class="short">
<option value="">Please Choose</option>
<option value="Agent" <?php if($account_type == 'Agent') echo "selected"?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') echo "selected"?>>Dealer</option>
</select>
<br><?php echo $error['account_type_error']; ?></p>
<p><label for="vat_number">VAT Number</label> <input class="short" value="<?php echo $vat_number; ?>" name="vat_number" type="text" id="vat_number" /><br><?php echo $error['vat_number_error']; ?></p>
<p class="submit"><input type="submit" name="submit" value="Update" /></p>
</fieldset>
</form>
<? echo ob_get_clean(); ?>
<?

} 
else {
        while ($row = mysql_fetch_array($sql_result)){
        $account_type = $row["account_type"];
        $vat_number = $row["vat_number"];
?>
<form name="details" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Details</legend>
<p>Please enter your details below, read the notes carefully.</p>
<p><label for="account_type">Account Type</label> 
<select name="account_type" class="short">
<option value="">Please Choose</option>
<option value="Agent" <?php if($account_type == 'Agent') echo "selected"?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') echo "selected"?>>Dealer</option>
</select>
<br><?php echo $error['account_type_error']; ?></p>
<p><label for="vat_number">VAT Number</label> <input class="short" value="<?php echo $vat_number; ?>" name="vat_number" type="text" id="vat_number" /><br><?php echo $error['vat_number_error']; ?></p>
<p class="submit"><input type="submit" name="submit" value="Update" /></p>
</fieldset>
</form>
<?
}
}
?>

Link to comment
Share on other sites

couple of other things:

$sql = "SELECT * FROM details WHERE fishery_id = '$_SESSION[fishery_id]' "; 
$sql_result = mysql_query($sql); 

You should change these to:

$sql = 'SELECT * FROM details WHERE fishery_id = \''.$_SESSION['fishery_id'].'\''; 
$sql_result = mysql_query($sql) or die (mysql_error());

Open all your PHP statements with

<?php 

as not all servers support the abreviated

<? 

And please think about using the specific column names in the SELECT rather than * (it's a very good habit to get into)

 

Can you post your form code?

Link to comment
Share on other sites

Hi Firstly thanks for the tips, just out of interest why is it best to change to that way with the sql query.

 

I understand about enclosing the php correctly, just missed that one.

 

The form was in the original post. But here is the form again without the selected change on. I have 2 forms one for the first time submit and one for when they return to make a change. I know this may not be the best method, but it works for me and at this stage I just need to get it working, appreciate your time.

 

<form name="details" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Details</legend>
<p>Please enter your details below, read the notes carefully.</p>
<p><label for="account_type">Account Type</label> 
<select name="account_type" class="short">
<option value="">Please Choose</option>
<option value="Agent" <?php if($account_type == 'Agent') echo "selected"?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') echo "selected"?>>Dealer</option>
</select>
<br><?php echo $error['account_type_error']; ?></p>
<p><label for="vat_number">VAT Number</label> <input class="short" value="<?php echo $vat_number; ?>" name="vat_number" type="text" id="vat_number" /><br><?php echo $error['vat_number_error']; ?></p>
<p class="submit"><input type="submit" name="submit" value="Update" /></p>
</fieldset>
</form>
<? echo ob_get_clean(); ?>
<?

} 
else {
        while ($row = mysql_fetch_array($sql_result)){
        $account_type = $row["account_type"];
        $vat_number = $row["vat_number"];
?>
<form name="details" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Details</legend>
<p>Please enter your details below, read the notes carefully.</p>
<p><label for="account_type">Account Type</label> 
<select name="account_type" class="short">
<option value="">Please Choose</option>
<option value="Agent" <?php if($account_type == 'Agent') echo "selected"?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') echo "selected"?>>Dealer</option>
</select>
<br><?php echo $error['account_type_error']; ?></p>
<p><label for="vat_number">VAT Number</label> <input class="short" value="<?php echo $vat_number; ?>" name="vat_number" type="text" id="vat_number" /><br><?php echo $error['vat_number_error']; ?></p>
<p class="submit"><input type="submit" name="submit" value="Update" /></p>
</fieldset>
</form>

 

 

Link to comment
Share on other sites

I wasn't paying enough attention.

) echo 'selected="selected"' ?>

should be

) {echo 'selected="selected"'} ?>

 

As for why change the format? It's much cleaner to break it up like that, makes it easier to follow the code and identify the php variables and can help avoid errors in some occasions

Link to comment
Share on other sites

Sorry I fixed that issue by putting the ; after the ' see below:

<option value="Agent" <?php if($account_type == 'Agent') {echo 'selected="selected"';} ?>>Agent</option>

 

I have tested it now and it still keeps defaulting back to the original status.

Link to comment
Share on other sites

Further update on this. I have almost got it working. The code that I was given from muddy does work to a degree. This is the issue I have now.

 

If the user first selects agent it does not require the vat number.

 

If the user changes agent to dealer it tells them it needs the vat number but the drop down select goes back to its original state with agent selected.

 

Now what will happen is they might not realise that it has gone back to agent and just enter the vat number and submit.

 

This is where the major problem lies, we will not know that they are a dealer even though the vat number is there? Is there a way around this?

Link to comment
Share on other sites

I tried adding the following as well. But if you have chose agent it will not let you switch to dealer.

 

<?php if ( $account_type =="Dealer") 
{
echo '<p><label for="vat_number">VAT Number</label> <input class="short" value="'.$vat_number.'" name="vat_number" type="text" id="vat_number" /><br>'.$error['vat_number_error'].'</p>';
}
?>

 

Just thought would show that above. Anyway, here is the code almost almost there, its the issue of coming back and trying to change.

 

<?php
if ( $_POST['submit'] ) {
$account_type = $_POST['account_type'];
$vat_number = $_POST['vat_number'];

if ( empty($account_type) ) {
  $error['account_type_error'] = '<div class="formerror">Please select your Account Type.</div>';
}
	if ( empty($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">In order for Pegbookers to act as a dealer on your behalf you must enter a correct VAT number.</div>';
		}
	elseif (!validVatnumberChars ($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">Please enter your VAT number as 123456789 (9 digits only).</div>';
	}
	elseif (!validVatnumberLength ($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">In order for Pegbookers to act as a dealer on your behalf you must enter a correct VAT number</div>';
	}
	elseif (!is_numeric($vat_number) AND ($account_type=="Dealer")) {
	$error['vat_number_error'] = '<div class="formerror">Please enter numbers only.</div>';
	}

$sql = 'SELECT * FROM fishery_e_bank WHERE fishery_id = \''.$_SESSION['fishery_id'].'\''; 
$sql_result = mysql_query($sql) or die (mysql_error());

if (mysql_num_rows($sql_result) ==0)
{
ob_start()
?>
<form name="fishery_bank" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Fishery Bank Details</legend>
<p>Please enter your details below, read the notes carefully.</p>
<p><label for="account_type">Account Type</label> 
<select name="account_type" class="short">
<option value="">Please Choose</option>
<option value="Agent" <?php if($account_type == 'Agent') {echo 'selected="selected"';} ?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') {echo 'selected="selected"';} ?>>Dealer</option>
</select>
<br><?php echo $error['account_type_error']; ?></p>
<p><label for="vat_number">VAT Number</label> <input class="short" value="<?php echo $vat_number; ?>" name="vat_number" type="text" id="vat_number" /><br><?php echo $error['vat_number_error']; ?></p>
<p class="submit"><input type="submit" name="submit" value="Update" /></p>
</fieldset>
</form>
<?php echo ob_get_clean(); ?>
<?php

} 
else {
       while ($row = mysql_fetch_array($sql_result)){
        $account_type = $row["account_type"];
        $vat_number = $row["vat_number"];
?>
<form name="fishery_bank" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Fishery Bank Details</legend>
<p>Please enter your details below, read the notes carefully.</p>
<p><label for="account_type">Account Type</label> 
<select name="account_type" class="short">
<option value="">Please Choose</option>
<option value="Agent" <?php if($account_type == 'Agent') {echo 'selected="selected"';} ?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') {echo 'selected="selected"';} ?>>Dealer</option>
</select>
<br><?php echo $error['account_type_error']; ?></p>
<p><label for="vat_number">VAT Number</label> <input class="short" value="<?php echo $vat_number; ?>" name="vat_number" type="text" id="vat_number" /><br><?php echo $error['vat_number_error']; ?></p>
<p class="submit"><input type="submit" name="submit" value="Update" /></p>
</fieldset>
</form>
<?php
}
}
?>

Link to comment
Share on other sites

Do you mean this as its the same as the first form?

 

<option value="Agent" <?php if($account_type == 'Agent') {echo 'selected="selected"';} ?>>Agent</option>
<option value="Dealer" <?php if($account_type == 'Dealer') {echo 'selected="selected"';} ?>>Dealer</option>

 

Link to comment
Share on other sites

Hi Muddy,

 

More about how this system works. First the user registers for an account which is seperate form etc.. and works fine. Then then fill in relevant information with this form being the problem I have.

 

The post submit is where the error check kicks in, pointless otherwise. So when the user clicks the submit button it executes the script, but like I say if you choose agent and try and switch to dealer it does not as agent is already in the database.

 

 

Link to comment
Share on other sites

right, as the POST['submit'] is coming from a different form, when you do a clean reload of this page you are likely going to loose the value and as such the conditions that apply to it are going to get a bit confused. See if this helps:

<?php
if ( isset($_POST['submit']) ) {
$_SESSION['submit'] = $_POST['submit'];
}
elseif (isset($_POST['submit']) || isset($_SESSION['submit']){
$account_type = $_POST['account_type'];
$vat_number = $_POST['vat_number'];

Link to comment
Share on other sites

Hi Muddy... sorry think I might not be clear.

 

Breakdown of how this works.

 

1: User registers - sent email that activate account and have a user and pass to log in

2: They go through around 5 different forms to fill in and can edit at any time by coming back and logging in

3: Every other form is fine, its just this one with the if else query that has the problem.

 

This form

1: The user fills in the form and clicks submit if no errors it then enters the information into the table in mysql.

2: The user comes back and wants to change their status from agent to dealer (dealer then requires the vat number)

3: If they select dealer and hit submit on the form it prompts them for the vat number (error check)

4: BUT the select option goes back to the default being Agent as that was what they first entered.

 

So the problem is just that, it will not keep the state of dealer. Does that make sense??????

 

The post submit is from the same form, the page has 2 forms but they are the same and both have the same submit option on them. So when the user hits submit it goes through the error check each time.

 

 

Link to comment
Share on other sites

Sorry, was feeding the family.  I'm stumped, if the page is completly self contained I can't see where the issue would lie.  I'd need to totaly re-write the script to work it out, somthing I doubt would be practical or desirable from your point of view.  I can only wish you the best of luck finding somone else more capable than me to work it out.

Link to comment
Share on other sites

HI Muddy,

 

I have been trying different things and its not working as something goes wrong each time. Thanks for your input.

 

I think it keeps pre populating with that agent field as when you submit it requests what is already in the database for the drop down with the sql query. I tried to do the following:

<option value="Dealer" <?php if(($account_type == 'Dealer') || ($account_type == $_POST['Dealer'])) {echo 'selected="selected"';} ?>>Dealer</option>

 

 

But again keeps defaulting back to agent.

 

Its the error check that is causing it revert back so I wonder if there is a way or changing that. I did reduce the error check to this for the vat number

 

if ($account_type=="Dealer") {
  if ( empty($vat_number)) {
  $error['vat_number_error'] = '<div class="formerror">In order for Pegbookers to act as a dealer on your behalf you must enter a correct VAT number.</div>';
  }
  elseif (!validVatnumberChars ($vat_number)) {
  $error['vat_number_error'] = '<div class="formerror">Please enter your VAT number as 123456789 (9 digits only).</div>';
  }
  elseif (!validVatnumberLength ($vat_number)) {
  $error['vat_number_error'] = '<div class="formerror">In order for Pegbookers to act as a dealer on your behalf you must enter a correct VAT number</div>';
  }
  elseif (!is_numeric($vat_number)) {
  $error['vat_number_error'] = '<div class="formerror">Please enter numbers only.</div>';
  }
}

 

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.