I have the last url segment of the following url
http://something.com/somefunc/10
that is the number 10;
My real problem is that I have a form that I need to post an action of the url
<form action="../something/somefunc/<?php echo $id>">
//some code
</form>
after receiving the posted stuff, I would like to still be able to get the id of the product under consideration. I think now that form's action url is the only way I know that can help retain such an id. Any help is appreciated, thank you.
[UPDATE]
Uhmmm, all the answers below are correct, don't know which one to choose as the best reply.
Something along these lines may work...
$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$path = $parts['path'];
$keys = explode('/', $path);
echo $keys[4]; // `corresponding to the 4th url word`
Hope this helps!
1.You could pass the product id using POST as a hidden input in your form:
<input name="productid" id="productid" type="hidden" value="<?=$id ?>">
and in the page you post to:
<? if (isset($_POST['productid'])) $id = $_POST['productid'] ?>
2.Alternatively use GET & pass it on the querystring
action="../something/somefunc/[processingpage]?productid=<?=$id ?>"
and in [processingpage]:
<? if (isset($_GET['productid'])) $id = $_GET['productid'] ?>
Sorry if I'm misunderstanding, but can't you get the ID of your posted form via the $_POST array?
http://www.w3schools.com/php/php_post.asp
Alternately, if the ID is in a URL parameter, you can access it via the $_GET variable:
http://www.w3schools.com/php/php_get.asp
You can put a hidden input with the id
<input type="hidden" name="id" value="<?php echo $id; ?>">
Related
How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value
This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.
If this is for validation checks no need for sessions as suggested.
User fills in the form and submits back to self
Sever side validation fails
$_GET
<input type="hidden" name="first"
value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />
validation message, end.
alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....
$_POST
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.
The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.
If $post is a normal variable, then it will never be saved.
If you need to save something, you need to use cookies. $_SESSION is an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.
Reference: http://php.net/manual/en/reserved.variables.session.php
The $_SESSION variable is just an associative array, so to use it, simply do something like:
$_SESSION['foo'] = $bar
You could save your $_POST values inside of $_SESSION's
Save your all $_POST's like this:
<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>
Actually in html forms it keeps post data.
this is valuble when you need to keep inserted data in the textboxes.
<form>
<input type="text" name="student_name" value="<?php echo
isset($_POST['student_name']) ? $_POST['student_name']:'';
?>">
</form>
put post values to session
session_start();
$_SESSION["POST_VARS"]=$_POST;
and you can fetch this value in another page like
session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];
You can use the same value that you got in the POST inside the form, this way, when you submit it - it'll stay there.
An little example:
<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>
You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily
if($_POST['name'])
{
$file = fopen('poststored.txt','wb');
fwrite($file,''.$_POST['value'].'');
fclose($file);
}
if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}
so your post value stored in $value.
i have a multiple choice quiz which sends a possible answer via URL like this
<form method="post" action="test2score.php?q=<?php echo $quiz_id; ?>">
on the test2score.php page it reads and tries to compare the value like this
$ans = mysql_result(mysql_query('select c_answer from quiz where quiz_id = "'.$_GET['q'].'"'),0);
if ($_POST['answer'] == $ans){
echo "You got it right!";
}else if ($_POST['answer'] <> $ans){
echo "You got it wrong!";
}
where c_answer is the correct answer stored in db, but even when i select the right answer and post it it still echoes "you got it wrong"
any help would be appreciated.
Your form method="post" is post and your receiving values in $_GET['q'] Kindly correct either of the one
Or use
$_REQUEST['q']`
Use a hidden field q and post that value to your action page and receive that value using
$_POST['q'] and use that in your query.
Thanks.
Why are you using a $_GET request in the action of a form that is $_POST? Since you aren't worried about your users seeing the value of q then why not just put a hidden input in the form with the results of q:
<input type="hidden" value=<?php echo $quiz_id ?> name="q" />
Then in your query, check for $_POST['q']
Seems the saner, more logical way to achieve this
instead of passing quiz id from form action pass it from hidden form field
like below
set form action to
action="test2score.php"
and take hidden form field as
<input type="hidden" name="q" value=<?php echo $quiz_id; ?> />
and get that value like below
$_POST['q'];
Users of my website can generate a custom form. All the fields are saved in a database with a unique ID. When someone visits the form, the fields 'name' attribute is field*ID*, for example
<p>Your favorite band? <input type="text" name="field28"></p>
<p>Your Favorite color? <input type="text" name="field30"></p>
After submitting the form, I use php to validate the form, but I don't know retrieve the value of $_POST[field28] (or whatever number the field has).
<?
while($field = $query_formfields->fetch(PDO::FETCH_ASSOC))
{
$id = $field[id];
//this doesn't work!!
$user_input = $_POST[field$id];
//validation comes here
}
?>
If anybody can help me out, it's really appreciated!
Add some quotes:
$user_input = $_POST["field$id"];
I'd suggest taking advantage of PHP's array syntax for forms:
<input type="text' name="field[28]" />
You can access this in php with $_GET['field'][28]
$user_input = $_POST['field'.$id];
Remember that you are using a string for the first part of the input name, so try something like: $user_input=$_POST['field'.$id];.
Also, I would suggest calling them into an array to retrieve all data:
<?php
$user_inputs=array();
while($field=$query_formfields->fetch(PDO::FETCH_ASSOC)) {
$id=$field['id'];
$user_inputs[]=$_POST['field'.$id];
}
?>
header('Location: ' . $uri);
This will miss all the $_POST information.
Don't use $_SESSION as you have been suggested. Session data is shared with all other pages, including the ones open in other tabs. You may get unpredictable behaviour if you use the same trick in multiple places of your website.
An untested better code would be something like this.
session_start();
$data_id = md5( time().microtime().rand(0,100) );
$_SESSION["POSTDATA_$data_id"] = $_POST;
header('Location: ' . $uri."?data_id=$data_id");
In the next page you may retrieve the previous post like this
session_start();
$post = array();
$data_key = 'POSTDATA_'.$_GET['data_id'];
if ( !empty ( $_GET['data_id'] ) && !empty( $_SESSION[$data_key] ))
{
$post = $_SESSION[$data_key];
unset ( $_SESSION[$data_key] );
}
The code above is not tested, you may have to deal with some error before it works.
if u want to carry forward your POST data to another pages ( except the action page) then use
session_start();
$_SESSION['post_data'] = $_POST;
Indeed, you can't redirect POST requests.
Either let your server proxy the request (i.e. make a cURL request to the other site) or create another form, fill it with hidden fields and submit it with Javascript/let the user click.
Alternatively, as #diEcho says, depending on what you're trying to do: sessions.
If you perform a redirect the post will be lost and a GET will occur.
You could save your POST in a SESSION or encode it in the GET (as query string)
You could save the post data in the session, redirect, and then retrieve it back from the session.
session_start();
$_SESSION['POSTDATA'] = $_POST;
header('Location: ' . $uri);
Then in the PHP file for the new location, retrieve the post data like this:
$_POST = $_SESSION['POSTDATA'];
I do use my own simple method.
Just think very logical! For example if you use a text attribute which you want to keep the value of, you can use:
<?php $textvalue = $_POST['textname']; ?>
<input type="text" name="textname" value="<?php echo $textvalue; ?>" />
<br />
//And if you want to use this method for a radio button, you can use:
<?php if(isset($_POST['radio1']){
$selected = "selected";
} ?>
<input type="radio" name="radio1" <?php echo $selected; ?> />
dear all,
I need to send parameters to a URL without using form in php and get value from that page.We can easily send parameters using form like this:
<html>
<form action="http://..../abc.php" method="get">
<input name="id" type="text" />
<input name="name" type="text"/>
<input type="submit" value="press" />
</form>
</html>
But i already have value like this
<?php
$id="123";
$name="blahblah";
?>
Now i need to send values to http://..../abc.php without using form.when the 2 value send to abc.php link then it's show a value OK.Now i have to collect the "OK" msg from abc.php and print on my current page.
i need to auto execute the code.when user enter into the page those value automatically send to a the url.So i can't use form or href. because form and href need extra one click.
Is their any kind heart who can help me to solve this issue?
You can pass values via GET using a hyperlink:
<a href='abc.php?id=123&name=blahblah' />
print_r($_GET) would then give you the values, or you can use $_GET['id'] etc in abc.php
Other approaches, depending on your needs, include using AJAX to POST/GET the request asynchronously, or using include/require to pull in abc.php if it only includes specific functioanlity.eg:
$id="123";
$name="blahblah";
require('abc.php');
You can do:
$id="123";
$name="blahblah";
echo "<a href = 'http://foo.com/abc.php?id=$id&name=$name'> link </a>";
<?php
$base = 'http://example.com/abc.php';
$id="123";
$name="blahblah";
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;