zimmo Posted May 5, 2010 Share Posted May 5, 2010 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> <? } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/ Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 Your form option echo should be ) echo 'selected="selected"' ?> in order to identify which option is shown on creation, otherwise it will default to the first option (in this case "Agent") Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053558 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 I have just changed that and it is still defaulting back to the original? Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053563 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053593 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053602 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053624 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 I am now getting a syntax error using that code. Appreciate your time. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053631 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 what line? Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053639 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053642 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053672 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 Can you re-post your new 'almost' working code? Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053674 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053680 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 you havn't changed the echo's on the second form - the one after the else statement. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053697 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053700 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 scratch that last one - I coppied the wrong code Can you tell me where the $_POST['submit'] ? Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053705 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053707 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 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']; Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053720 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053728 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 Sorry to bother people, but I am really stuck on a solution here. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053758 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053783 Share on other sites More sharing options...
zimmo Posted May 5, 2010 Author Share Posted May 5, 2010 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/200784-if-else-not-allowing-me-to-change/#findComment-1053787 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.