Results 1 to 11 of 11

Thread: [RESOLVED] $_REQUEST then $_GET??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] $_REQUEST then $_GET??

    I'm recording numbers on my database using a textfield, $_REQUEST and $_GET commands.

    I have this code
    PHP Code:
    if(isset($_REQUEST['Cost']))
    $each_cost=$_REQUEST['Cost']; //array of costs entered
    else if(isset($_GET['Cost']))
    $each_cost=$_GET['Cost']; 
    The inputed cost is recorded in the URL string and called on later when adding multiple elements to my database.
    PHP Code:
    foreach($each_cost as $key => $value){ $cost_string =  $cost_string."&Cost[$key]=".$value; } 
    I'm finding that saving the '$cost_string' requires me to refresh the current page twice for the string to record the entered number correctly?

    Any idear why?

    I've noticed that the 'Cost' (in the URL) only appears after I've refreshed the page twice but I've also used the $_REQUEST function and It still isn't working?

  2. #2
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: $_REQUEST then $_GET??

    I think that you will need to show more code to find out why. You might want to check that you are getting the value of it before you are trying to use it. Can you show more code?
    If I helped you please rate me.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: $_REQUEST then $_GET??

    why are you using the $_REQUEST global? $_REQUEST combines the get/post/cookie variables. I'm not sure why you would do this logically -- if $_GET['cost'] is set, then $_REQUEST['cost'] is set. if you're not posting to this script, then using that global isn't really going to change anything. if you are, then you only need to check if $_REQUEST['cost'] is set at all. otherwise, I'd suggest being specific and use the right array; either $_POST, $_GET or $_COOKIE.

    anyway! it does sound like you might need to post a little more code. I can't see what's wrong with the lines you posted.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: $_REQUEST then $_GET??

    text field...
    PHP Code:
    <form name="myform" method="post" action="<?php
                                      $Costno 
    // amount of text fields.
                                      
    $no=0;            
                      while(
    $no<$Costno){
                      echo 
    "&Cost[$no]=$costtotal[$no]";
                      
    $no++;}
                      
    ?>" >
    <input name="Cost[<?Php echo "$key"?>]" type="text" value="<?php 
                  
                  
    echo "$each_cost[$key]";
                  
                  
    ?>" size="5" /></form>
    submit button...
    HTML Code:
    <form method="post" >
                <input name="Save" type="submit" class="box" value="Save" />
              </form>
    calculate and show results
    PHP Code:
    <?php 
                  
                   
    echo "£".array_sum($costtotal);
                   
                   
    ?>
    reconstruct URL string and save.
    PHP Code:
    foreach($each_cost as $key => $value){ $cost_string =  $cost_string."&Cost[$key]=".$value; }

    if (
    $_POST['Save']){
    mysql_select_db("Sums"$con);

    mysql_query("INSERT INTO DB (Cost) VALUES ($cost_string)"); 

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: $_REQUEST then $_GET??

    foregoing some small problems I see with your script, it looks like it'd be way easier for you to just submit your "cost" form via GET instead of POST. you're submitting a form via POST with a GET request as the action, and you want the values of that form in the GET array. why bother doing the work? let PHP do that for you.
    PHP Code:
    <form method="get" action="this_script.php">
    <?php for($i 0$i 3$i++): ?>
      <input type="text" name="cost[<?php echo $i?>]" value="<?php echo htmlentities($_GET['cost'][$i]); ?>" />
    <?php endfor; ?>
      <input type="submit" value="sum values" />
    </form>
    this will populate your URL correctly, and will correctly re-populate it every submission. then, you can reconstruct it later on.

    also, if you're not going to be manipulating the URL later on (and don't have your array filled with a bunch of other stuff) and just need to save the entire query string, you could simple store $cost_string like this:
    PHP Code:
    $cost_string $_SERVER['QUERY_STRING']; 

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: $_REQUEST then $_GET??

    Changing the form from POST to GET would make life easyer but I am using the URL for other stuff! how can I add the form information to whats in the URL already?

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: $_REQUEST then $_GET??

    you could loop through the contents of GET and make hidden inputs if you want.
    PHP Code:
    <?php
      
    foreach($_GET as $key => $value):
        if(
    $key == "cost") continue; //skip "cost"
    ?>
    <input type="hidden" name="<?php echo $key?>" value="<?php echo htmlentities($value); ?>" />
    <?php endforeach; ?>
    this should do basically what you're looking for. though, it won't help if you have other arrays in your URL, but you could use a modified method of this.

  8. #8
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: $_REQUEST then $_GET??

    Just wondering, why are you using GET?
    I know there are cases in which GET is a better options, but most of the times you can do the same with POST.
    Delete it. They just clutter threads anyway.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: $_REQUEST then $_GET??

    I find that using the code produces '%3D' and '%26' in the URL. How can I remove these?

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: $_REQUEST then $_GET??

    Quote Originally Posted by LingoOutsider View Post
    I find that using the code produces '%3D' and '%26' in the URL. How can I remove these?
    Which code are you using?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: $_REQUEST then $_GET??

    Sorry my error its working now

Posting Permissions

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



Click Here to Expand Forum to Full Width