Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 37

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

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

    Default

    You could use cookies if you prefer. Sessions are fairly simple to work with-- just be sure to keep session_start() at the top of every single page and then $_SESSION is an array you can use between pages for the duration of the stay.

    As for which I was talking about, you can do both. Point to a single page as needed, and use variables in the URL to specify info; also use $_POST to confirm that data was sent and do different things based on that. Most of that is just based on layout.

    Think about a complex forum (like this one*). Everything is run through the same index page and then various actions are performed, usually based on the URL variables but also based on submitted forms and things like whether the user is logged in, etc. The single script then parses everything and serves whatever content should be done by a variety of conditional (if) statements, functions and includes(), sometimes including up to hundreds of pages in a single "script".

    (*vbulletin is in the minority of forum software, running several processes through different pages than index.php but it's all still going through the same setup in a complex way; it still follows the same basic principles.)


    As always, if it works, then just keep it simple, but this is stuff to think about for the future.
    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

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

    Default

    I've never worked with cookies either. I'm not a php programmer. I can get things to work only when I have explicit examples to follow.

    I would like to put the action code in the same file as the form but have no clue how to do it.

    I'd like to try using sessions for the agent issue but these two lines seem to cancel each other out so it doesn't make sense to me...

    Code:
    <?php
    $agent = array('DD'=>'Debbie','JV'=>'Joanne','HS'=>'House');
    
    //immediately above the foreach loop, add this:
    $agent = '';

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

    Default

    Oh, yes, I forgot about the $agent variable. Just rename one to something else. They're for different purposes.
    Call the new one $currentagent for example.

    I understand it can be difficult. This type of situation is where you choose whether it's better to just get it working or to perfect it. Especially for a page that gets limited use (admin pages are a perfect example) I see no point in perfecting it, though as I said it's worth thinking about these ideas for later because you'll probably run into the same general ideas.


    If you want to get an idea of a single page doing a lot, consider looking at some free forum software and how it's all setup. It's IMMENSELY complex, but you can get a feel for the whole layout and see some of the syntax for how it all relates.

    Basically you use exactly the same tools you've been using, but just larger and larger code blocks and many more levels-- 1000s of lines code and sometimes a dozen tabs in (within ifs and loops, etc).
    Of course this specific case wouldn't be anywhere near that complex, but it's the same idea and method-- just build up the same stuff you do know and you'll find you've made a very complex page.
    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

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

    Default

    And then you said...

    Code:
    //then replace this line:
     if  (isset($_POST['agent']) && $_POST['agent'] == $agentv)) { 
    //with:
     if ($agent==$agentv) {...
    but there is no line like that, only a line like this...

    Code:
    $selected = (isset($_POST['agent']) && $_POST['agent'] == $agentv)?' selected':'';
    Is that the line you meant? And if so, I should replace it with what? I have to take what you say literally and do not know enough to recognize if you meant to say something else. I would really appreciate if you could please put the code in one chunk with everything the way you meant to say it. I would really like to get this working properly.

    And this is not for the admin page for a website. It is for a custom app that will be used to run an art gallery. The invoices are piling up while they wait for me to design the CMS, clean up 30 years of data, and write the app. The pressure is killing me.

    I am trying to simplify, not make things more complex. In that car rental app you helped me with, I put all the "write to database" modules in one little file no problem. I was just hoping to do the same thing here, but maybe it doesn't work with form actions.

    I really appreciate your efforts to help me. God knows I need all the help I can get!

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

    Default

    Sorry, I had accidentally cut and paste from the code that bluewalrus wrote "translating" my original code. Yes, that's the right line.
    $selected = ($currentagent == $agentv)?' selected''';

    (where $currentagent is that new variable I made...)


    It's always a tradeoff whether you want to make things more complex, streamlined and in general "advanced". If it works, don't break it by trying to fix it.

    Putting anything into a single page is very possible including form actions. Just output different pages based on what is going on.

    The easiest way to get started with this is to comment your entire script and clean up the code as much as you can (tabs, extra lines between different sections) and maybe split it into different files to be included.
    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

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

    kuau (04-27-2010)

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

    Default

    Can you please tell me what is wrong with this statement? Or if there is a better way to do it? Thanks.

    This does work without the thousands separator...

    Code:
    <?php $result = mysql_query("SELECT COUNT(*) FROM client");?><b><?php echo mysql_result($result,0,0);?>

    This doesn't work when I try to add a thousands separator...

    Code:
    <?php $result = mysql_query("SELECT COUNT(*) FROM client");?><b><?php echo mysql_result(number_format($result,0,0));?>

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

    Default

    Isn't that just layered in the wrong order?

    mysql_result() gets the data, THEN you want to format is using number_format().
    So just do number_format(mysql_result(...))?
    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

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

    kuau (04-27-2010)

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

    Default

    Brilliant.. that was it! Mahalo!!

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

    Default

    Hey Daniel:

    You're not going to believe this but I figured out how to get the drop-down to default to the last agent entered! It's no doubt a horrible adulteration of the php language but it works...

    I had to change both forms to POST instead of GET, then I added this to the action file of the second form...
    Code:
    if($_POST['agent'] == 'DD'){
      header("Location: /admin/php/client-history-add.php?agent=DD"); 
    } elseif($_POST['agent'] == 'JV'){
      header("Location: /admin/php/client-history-add.php?agent=JV");
    } else {
      header("Location: /admin/php/client-history-add.php?agent=HS");
    }
    Then I put this at the top of the form file...

    Code:
    <?php if(isset($_GET['agent']) && $_GET['agent'] == "DD"){ $ddsel = " selected"; 
    } elseif(isset($_GET['agent']) && $_GET['agent'] == "JV"){ $jvsel = " selected";
    } elseif(isset($_GET['agent']) && $_GET['agent'] == "HS"){ $hssel = " selected"; } 
    ?>
    And this in the form...

    Code:
    <select name="agent">
    <option value="JV"<?php echo $jvsel;?>>Joanne</option>
    <option value="DD"<?php echo $ddsel;?>>Debbie</option>
    <option value="HS"<?php echo $hssel;?>>House</option>
    </select></p>
    And that's it! Works like a charm. It came to me while I was running. Feel free to tell me what's wrong about it. Thanks, e

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

    Default

    That's a really simple way to do it, actually. Nothing's wrong with it. It's just not expandable/variable. If you do it in a way like I did before it was completely customizable and expandable to more names, etc. But for your purposes that's simpler.
    And of course the main problem is that you must include that extra variable in every URL, so pay attention to that.
    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

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
  •