SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 46 of 46
  1. #26
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, everything correct. I understood all allright.

    You have some code in your script, which adds \r\n to your data.
    You must get rid if it.

    But if you tired of it, then okay, do your str replace.
    But note it does replace some more symbols too.

  2. #27
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I will live with str_replace for the moment. But now i come to the last problem in my validation!
    I have two select menus in the whole form (I've been doing things bit by bit so far but now I'm getting to the last parts). I think part of the problem is that I have to select the options from a mysql database first, then print them in the options - which works fine - but then I cannot replicate the choice made by the customer when the form is validated.
    The code for one of the select menus is as follows:
    Code HTML4Strict:
    <td class= "content">
    <SELECT NAME="region_name">
    <OPTION SELECTED VALUE=""> Select One
    <?php
    //first get the names of the regions
    $sql1 = "SELECT region_name FROM tbl_region";
    $result1 = mysql_query($sql1);
    if ($result1 = mysql_query($sql1)) {
    if (mysql_num_rows($result1)){
    	while ($row = mysql_fetch_assoc($result1)) {
    	echo "<OPTION VALUE=\"{$row['region_name']}\">{$row['region_name']}</option>";
    }
    }
    }
    ?>
    </SELECT>
    <p class= "error"><?php echo $error8; ?></p></td>
    Where should I put the <?php echo?> for the customer's selection? Or will I have to do things a different way? I've tried several ways but they don't work and when the form is validated the region selection just comes up with "select one" as in the empty form.

    thanks

  3. #28
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes. different way
    do you know at least what HTML you want to get?

  4. #29
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, the select menu should look like the attached image.
    So, I add the "select one" and then the options come from the mysql table. I can't do it with set options in the html according to the system spec. "Select one" shows in the form as it appears on the screen and then when the user clicks on the arrow the options drop down.
    I have set the error for this field to be:
    PHP Code:
    if ($region_name = ('Select One')) {
              
    $error true;
              
    $error8 errorRegionName;
          } 
    which would work if the user did not make a selection but when the form is validated it just defaults to "select one" - which gives an error but is not what should happen if the user has made a choice
    Attached Images Attached Images

  5. #30
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you know what HTML you need to have menu as displayed on attached picture?

  6. #31
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it's the html in the code above together with the php there. I assume that it is the ?<php echo that is causing the problem for the replication of the data but can't see how to change that. Is there a better way of displaying the menu from the results of the mysql query?

  7. #32
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    everything ok with this way of displaying.
    do you understand that this PHP+HTML code will produce HTML only code, which goes to the browser alone?
    do you know which HTML code you want in browser?

  8. #33
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mmmm not quite sure what you mean. I know that the results of the mysql only go to the browser. When I was testing the form before I tried validating as well, all the data I wanted was entered into the database. The problem is that in the customer table, where the contact info goes, the region-name only has an id which is generated from the region table so when I send the data from the form to the customer table I have to do a query on the region table to get the id of the chosen region to then insert it into the customer table with the rest of the data. So, surely it does not matter that the code I'm using for the menu in the form will only go to the browser? Or am I being very dense?

  9. #34
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while ($row = mysql_fetch_assoc($result1)) {
    if ($_POST['region_name']==$row['region_name']) $sel=" selected"; else $sel="";
    echo "<OPTION VALUE=\"{$row['region_name']}\"$sel>{$row['region_name']}</option>";

  10. #35
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that but I then get an error message that region_anme is an undefined index, so then I changed the last 2 rows around like this:
    Code PHP:
    <SELECT NAME="region_name">
    <OPTION SELECTED VALUE=""> Select One
    <?php
    //first get the names of the regions
    $sql1 = "SELECT region_name FROM tbl_region";
    $result1 = mysql_query($sql1);
    if ($result1 = mysql_query($sql1)) {
    if (mysql_num_rows($result1)){
    	while ($row = mysql_fetch_assoc($result1)) {
    echo "<OPTION VALUE=\"{$row['region_name']}\"$sel>{$row['region_name']}</option>";
    if ($_POST['region_name']==$row['region_name']) $sel=" selected"; else $sel="";
    }
    }
    }
    ?>
    </SELECT>
    and I then get an error message that sel is undefined. Have I used your code incorrectly?

  11. #36
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh yes, of course.
    everything the same

  12. #37
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I still get the error message "undefined index: region_name"
    the code I use is this:
    Code HTML4Strict:
    <SELECT NAME="region_name">
    <OPTION SELECTED VALUE=""> Select One
    <?php
    //first get the names of the regions
    $sql1 = "SELECT region_name FROM tbl_region";
    $result1 = mysql_query($sql1);
    if ($result1 = mysql_query($sql1)) {
    while ($row = mysql_fetch_assoc($result1)) {
    if ($_POST['region_name']==$row['region_name']) $sel=" selected"; else $sel="";
    echo "<OPTION VALUE=\"{$row['region_name']}\"$sel>{$row['region_name']}</option>";
    }
    }
    ?>
    </SELECT>

  13. #38
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea, i see
    if (isset($_POST['region_name']) AND $_POST['region_name']==$row['region_name']) $sel=" selected"; else $sel="";

    and change line above to
    <OPTION VALUE=""> Select One</OPTION>

  14. #39
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, that works! thank-you so much!
    But one little problem....
    I'm not sure what the $error condition for region_name should be. At the moment an error message comes up for that field even if a region has been selected.

  15. #40
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ugh. how do you check it?
    it should be whare all other conditions are

  16. #41
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which is where it is! However, where all the non-menu select fields, apart from the email address which checks for valid email address, use strlen() to check for validity, I obviously cannot use that for a menu field.
    Logic tells me that something like:
    Code PHP:
    if  ($region_name = ("Select One")) {
              $error = true;
              $error8 = errorRegionName;
          }
    should work but, although after validation the chosen menu item is retained, the error message that no selection has been made always appears. I've tried lots of other things but all with the same result...
    I know I am not very good at this but I am trying, I've come a long way from being a complete non-programmer and I'm learning all the time, your help is very much appreciated.

  17. #42
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahahaha
    what a silly mistake.
    == not single =
    hard to fihd though
    they advise to write it countrary, to prevent such mistakes
    ("Select One" == $region_name)
    with = instead of == or === parse error will be raised

  18. #43
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, not a silly mistake - a really stupid one!! Yes, it works fine now . Thanks for pointing out that very stupid mistake1

  19. #44
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't believe this! I was too hasty saying it works fine. It does in the sense that the error message does not come up when a selection has been made but if it is left as "select one" i.e. no selection is made then no error message comes up, so plainly it's not working!!
    Code PHP:
    if  ("Select One" == $region_name) {
              $error = true;
              $error8 = errorRegionName;
          }
    As you can see I've changed the order around as well as correcting the = but I cannot see why it is not working.

  20. #45
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just check what goes to your script from the form when no region selected
    it can be empty string or Select one with some deviations such as spaces or letter case.
    put var_dump($region_name); somethere, look at the output and correct that string to compare.

  21. #46
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! That solved the problem - I must remember to use var_dump() as a help in debugging in the future. Another lesson learned!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •