[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?
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?
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.
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)");
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'];
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?
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.
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.
Re: $_REQUEST then $_GET??
I find that using the code produces '%3D' and '%26' in the URL. How can I remove these?
Re: $_REQUEST then $_GET??
Quote:
Originally Posted by
LingoOutsider
I find that using the code produces '%3D' and '%26' in the URL. How can I remove these?
Which code are you using?:confused:
Re: $_REQUEST then $_GET??
Sorry my error its working now