Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 37

Thread: Is it possible to submit a form without reloading the page?

  1. #11
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Hey,

    Sorry to jump in our your thread Kuau. I was wondering about the code djr33 used "$selected = (isset($_POST['agent']) && $_POST['agent'] == $agentv)?' selected'':'';" . I think this says

    PHP Code:
    if  (isset($_POST['agent']) && $_POST['agent'] == $agentv)) {
    $selected "" ;
    } else {
    $selected "selected";

    but just wanted to make sure. I've only seen this used in javascript but it looks more efficient is there a name for this shorthand version? Thanks.

    As for your problem Kuau which line is line 55?
    Corrections to my coding/thoughts welcome.

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Kuau,
    1. In my code I intentionally left no space before $selected in the string. This is because there should only be a space if "Selected" is set (and the space is stored in it's value then). This will make the code cleaner in the HTML output. The extra space won't hurt at all, but if you want really clean HTML output, that's the only way to do it (leave no space and include it into the variable $selected as needed).
    It's always very difficult to get HTML to look nice after generating it with PHP. This often happens with tabs and then it can be a huge pain to deal with. The good news is that it never affects functionality, just pretty source code...

    2. I believe the problem is in the complex line setting $selected. You have at the end three single quotes: '''; That should be like in my post with a colon between them--- ':'';. This is very unusual syntax but it's the shorthand way to make it look clean (if confusing to read). I'll explain below:


    bluewalrus, I have no idea what it's called. I originally found it looking through something (forum software, I think) and tried to figure out what it was.

    It's probably the least readable and most extraneous thing in PHP, but it can make one line from a few and it's a quick way to set variables based on a single condition.

    Here's the syntax:
    (CONDITION) ? (TRUE) : (FALSE) ;
    Read as: "if CONDITION, do TRUE, else do FALSE", or however you'd like to phrase that.
    Or-- X?Y:Z; read as "if X then Y else Z"

    So your post is very close, but you have the if and else reversed-- if the conditions in the first bit (before the ?-- I look at it as "if this stuff true (.......)?") then the "selected" text will be used. Otherwise, just use a blank string.


    This syntax is useful for rare occasions like this one. Usually it's just annoying. I've even in some cases embedded several layers into one line when there's a complex mess of conditions to deal with, but that's so unreadable that I try to avoid it in most cases.
    That would look something like: ($x==1)?$y=1: (($y==1)?$y=2:$y=0);-- useless/random example.

    Note that I have no idea if all the parts are required or what happens if you omit one. You can use this as with anything else, just use parentheses and you can even include multiple operations separated by ;s. Also, I don't know what happens with/without parentheses-- it seems like it could get very confusing/ambiguous in certain circumstances.
    Last edited by djr33; 04-26-2010 at 06:48 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following 2 Users Say Thank You to djr33 For This Useful Post:

    bluewalrus (04-27-2010),kuau (05-04-2010)

  4. #13
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Finally I can answer someone's question!!!

    Blue Walrus, this shorthand form of the conditional IF statement is called the php ternary operator.

    I just got back from running on the beach (when I do my best thinking) and it occurred to me that perhaps Daniel's method could be used (?) to keep the client name in the drop-down (my original question above), because it takes too long to load the page the way we did it.

    Daniel: Thanks for the great explanation. I have to try it again and then it should make more sense. It amazes me that you can just whip something like that off so quickly. I need to stare at it for a while now. I'll let you know what happens. Mahalo plenty! e

  5. The Following User Says Thank You to kuau For This Useful Post:

    bluewalrus (04-27-2010)

  6. #14
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    OK, I tried it exactly as written and got an error on the select line so I removed one of the single quotes after selected at the end of the line here: $agentv)?' selected'''; and the error went away.

    But it is not functioning as intended. I added an entry as Debbie and when the page came up again, it said Joanne. Then I added some notes as Joanne then changed to Debbie and it came up Joanne again. I would like it to default to the last agent entered until it is changed and then stay as that agent until it is changed again. Any ideas?

  7. #15
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's more complex. $_POST refers to values sent from a form. You could add a hidden field to every page so that will always be available:
    <input type="hidden" name="agent" value="<?php echo $_POST['agent']; ?>">

    Or you can use cookies or sessions.

    So basically you'll do this:
    PHP Code:
    //immediately above the foreach loop, add this:
    $agent '';
    if (isset(
    $_SESSOIN['agent'])) { $agent=$_SESSION['agent']; }
    if (isset(
    $_POST['agent'])) { $agent=$_POST['agent']; $_SESSION['agent'] = $agent;}

    //then replace this line:
     
    if  (isset($_POST['agent']) && $_POST['agent'] == $agentv)) { 

    //with:
     
    if ($agent==$agentv) {... 
    Also, remember session_start(); at the top of your page.
    Alternatively replace $_SESSION in the above code with $_COOKIE and for the last bit use setcookie() rather than $_COOKIE['agent']=.....
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (04-27-2010)

  9. #16
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Daniel: I realized after I wrote the above that the action url was sending me back to the old page without the code so it really was no test of the code at all. I'm going to try it again as I am curious what it will do. What is the code supposed to do if not what I thought?

    Is there a way to include the action code, eg. <form name="historyform" method="POST" action="client-history-add-php.php" in the same page as the form? I tried putting all the actions in one file with an if($page="client-history-add.php"){ run action } but couldn't get the variable value to pass through. I don't like all these little files proliferating.

  10. #17
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yes it is possible. Use action="?page=name" and ifs with $_POST. It can get complex but that's where clean code helps. If you really want separation then you can use includes to separate the php code that will then process as one page or create functions for each action.
    It's possible to do this without a variable in the URL if you check isset($_POST['formvar']) to see if data was sent but that takes careful planning.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. The Following User Says Thank You to djr33 For This Useful Post:

    kuau (04-27-2010)

  12. #18
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Which is possible?...

    1) putting the action on the same page as the form, or
    2) putting all the actions in one file.

    You seem to be talking about the latter (?).

  13. #19
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I am trying to use the session code but I am so confused I don't know what to put where. Could you please put the whole code without the replace this with that, because I don't have the "this" exactly as you have it so it doesn't make sense.

    This is what I have:

    Code:
    <p><label>* Agent:</label>
    <select name="agent">
    
    <?php
    $agent = array('DD'=>'Debbie','JV'=>'Joanne','HS'=>'House');
    
    //immediately above the foreach loop, add this:
    $agent = '';
    if (isset($_SESSION['agent'])) { $agent=$_SESSION['agent']; }
    if (isset($_POST['agent'])) { $agent=$_POST['agent']; $_SESSION['agent'] = $agent;}
    
    //then replace this line:
     if  (isset($_POST['agent']) && $_POST['agent'] == $agentv)) { 
    
    //with:
     if ($agent==$agentv) {...  
    
    
    foreach ($agent as $agentv=>$agentn) {
    $selected = (isset($_POST['agent']) && $_POST['agent'] == $agentv)?' selected':'';
    echo "<option value=\"$agentv\"$selected>$agentn</option>";
    }
    ?>
    </select></p-->
    <input type="hidden" name="agent" value="<?php echo $_POST['agent']; ?>">
    <p><label>* Agent:</label><?php echo $_POST['agent']; ?>
    <select name="agent">
    <option value="<?php echo $_POST['agent']; ?>"><?php echo $_POST['agentn']; ?></option>
    <option value="JV">Joanne</option>
    <option value="DD">Debbie</option>
    <option value="HS">House</option>
    </select></p>

  14. #20
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I've given up. I made 2 copies of the whole page and put one with DD at the top and one with JV at the top. I now have 2 items on the menu so that there is a separate page for each agent. I know this sucks as a solution but at least it works. I can't afford to spend this much time on such a tiny, tiny detail. But thanks for all the help. I'm sorry that I am not able to follow your directions sometimes. You must think I know more than I do. I've never worked with sessions. This is so discouraging that I can't get the simplest thing working.

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
  •