I'm trying to use a textbox value as a session variable - php

I'm trying to use a textbox value as a session variable when the button is clicked, so far I have it working but it's hardcoded in, how do I get it to use the textbox value instead?
Here's the session code:
<?php
session_start();
$_SESSION['url'] = "url";
?>
Here's the textbox:
echo "<input type='text' id='starurl' value=''/>";
echo "<br><button onclick='save_a9({$row99['starID']})'>Approve</button><button onclick='save_d9({$row99['starID']})'>Disapprove</button><br>";
Here's the save_a9:
function save_a9(id) {
$.post('response6.php', {starID:id},
function(result) {
alert(result);
window.location.reload();
});
}
Is this what you mean? the value doesn't need a form, it just needs to go to another page to be used there
<?php
session_start();
if (isset($_POST['url']) {
$_SESSION['url'] = $_GET['url'];
}
?>
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url'])"

When you are assigning the _SESSION url, you will need to use the posted variable to assign the string.
$_SESSION['url'] = $_GET['url'];
To do the opposite, and have the textbox show the value of the session, you would:
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
It is important to note that if you want the text box to actually do something, you will need to have the input box wrapped around a form tag.
The <form> tag tells the browser where the form starts and ends. You can add all kinds of HTML tags between the <form> and </form> tags. (thanks echoecho!)
If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.
Some examples:
GET the page showing a particular SO question
POST a comment
Click the "Add to cart" button and send a POST request.
(Thanks Skilldrick!)
The form & PHP file would look like this:
<?php
session_start();
if (isset($_POST['url']) {
$_SESSION['url'] = $_POST['url'];
}
echo '<form action="POST" method="?">';
echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
echo '</form>';
You will notice when the form is updated, the session updates too. When you close your browser and open it again, you will see you old contents on the input box. (Press "enter" to save the input box without a submit button).

Simply use this one
//page1.php
<form action="page2.php" method="post">
<input type="text" name="session_value">
<input type="submit" name="set_session" value="Set Session">
</from>
//page2.php
<?php
if(isset($_POST['set_session'])){
session_start();
$_SESSION['url'] = $_POST['session_value'];
}
?>

For getting the value from your textbox, first modify your HTML as :
<input type='text' id='starurl' name='url' value=''/>
The textbox needs a name attribute which is used by the $_POST, $_GET or $_REQUEST superglobals.
Then depending upon your form submission method (GET or POST) :
$_SESSION['url'] = $_GET['url'] // Default method for form submission
$_SESSION['url'] = $_POST['url'] // For <form method="post">
$_SESSION['url'] = $_REQUEST['url'] // Works for both
EDIT :
Since you are using a button onclick event to set the session variable, you can use this (assuming you are using jQuery) :
Javascript:
$('button').click(function(){
var url = $('#starurl').val();
$('#sessionURLDiv').load('save_session_url.php?url='+url);
});
PHP: save_session_url.php :
<?php
session_start();
if ( isset ( $_GET['url'] ) ) {
$_SESSION['url'] = $_GET['url'];
}
?>
HTML :
Just add this div anywhere in the page.
<div id = "sessionURLDiv"></div>
I hope this helps.

Related

passing form input name through hyperlink

I am trying to pass input tag value to the another page to simply multiply the value of qty with price and return the updated price.The problem is when i pass the value of qty with get method it passes the value of qty only but does not pass another values.
cart.php
echo" <form method='get' name='form1' action='update_cart.php?id={$id}&name={$name}&price={$price}&qty=$_GET['qty']'>";//the problem comes here.
echo"<input type='number' name='qty' max='10'>
<input type='submit' value='update'></form>";
update_cart.php
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$qty=isset($_GET['qty'])? $_GET['qty']: "";
$price=isset($_GET['price'])? $_GET['price']: "";
$price=$price*$qty;
header('Location: cart.php?action=quantity_updated&id=' . $id . '&name=' . $name . '&price='.$price . '&qty='.$qty);
when i click on update button after giving qty a value it shows something like this.
http://localhost/abc/cart.php?action=quantity_updated&id=&name=&price=0&qty=2
form method="get" passes variables automatically.
You do (and can) not need to append query string to the action attribute.
You do however need to represent those data in your form, if with <input type="hidden" name="qty" value="<?=$qty?>" /> style.
if you want pass some values , you can choose 2 way for it :
you can use the input with hidden type in you form.
if you use hidden type input you can use data just on one page.
<form method='get' action='?????'>
<input type='hidden' name='?????' value='?????'>
<input type='hidden' name='?????' value='?????'>
....
...
..
<input type='number' name='qty' max='10'/>
<input type='submit' value='submit'/>
</form>
you can use session for save data and use it in another page.
if you use session, you can use data in all php pages.
on page 1:
<?php
session_start();
$_SESSION['name']='value';
?>
on page 2:
<?php
session_start();
echo $_SESSION['name']; // value
?>
Your syntax for this line contains errors.
echo" <form method='get' name='form1' action='update_cart.php?id={$id}&name={$name}&price={$price}&qty=$_GET['qty']'>";
Change it to...
echo" <form method='get' name='form1' action='update_cart.php?id=".$id."&name=".$name."&price=".$price."&qty=".$_GET['qty']."'>";

Using a variable across the pages in PHP [duplicate]

My website involves a user submitting data over several pages of forms. I can pass data submitted on one page straight to the next page, but how do I go about sending it to pages after that? Here's a very simplified version of what I'm doing.
Page 1:
<?php
echo "<form action='page2.php' method='post'>
Please enter your name: <input type='text' name='Name'/>
<input type='submit' value='Submit'/></form>";
?>
Page 2:
<?php
$name=$_POST["Name"];
echo "Hello $name!<br/>
<form action='page3.php' method='post'>
Please enter your request: <input type='text' name='Req'/>
<input type='submit' value='Submit'/></form>";
?>
Page 3:
<?php
echo "Thank you for your request, $name!";
?>
The final page is supposed to display the user's name, but obviously it won't work because I haven't passed that variable to the page. I can't have all data submitted on the same page for complicated reasons so I need to have everything split up. So how can I get this variable and others to carry over?
Use sessions:
session_start(); on every page
$_SESSION['name'] = $_POST['name'];
then on page3 you can echo $_SESSION['name']
You could store the data in a cookie on the user's client, which is abstracted into the concept of a session. See PHP session management.
if you don't want cookies or sessions:
use a hidden input field in second page and initialize the variable by posting it like:
page2----
$name=$_POST['name']; /// from page one
<form method="post" action="page3.php">
<input type="text" name="req">
<input type="hidden" name="holdname" value="<? echo "$name"?>">
////////you can start by making the field visible and see if it holds the value
</form>
page3----
$name=$_POST['holdname']; ////post the form in page 2
$req=$_POST['req']; ///// and the other field
echo "$name, Your name was successfully passed through 3 pages";
As mentioned by others, saving the data in SESSION is probably your best bet.
Alternatly you could add the data to a hidden field, to post it along:
page2:
<input type="hidden" name="username" value="<?php echo $name;?>"/>
page3
echo "hello $_POST['username'};
You can create sessions, and use posts. You could also use $_GET to get variables from the URL.
Remember, if you aren't using prepared statements, make sure you escape all user input...
Use SESSION variable or hidden input field
This is my workaround of this problem: instead of manually typing in hidden input fields, I just go foreach over $_POST:
foreach ($_POST as $key => $value) {
echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
Hope this helps those with lots of fields in $_POST :)

Why is global scope $_GET not seen?

One of my pages (video.php) is opened using form action as follows:
<?php
//Lots of code, including a WHILE loop
echo "<form action=\"video.php?id=".$row['id']."\" method=\"post\" target=\"_top\">
<input type=\"image\" src=\"".$image."\" style=\"width:180px;height:120px\"
alt=\"Submit\"></form>";
?>
On the page video.php?id, I get the id as follows and declare other global scope vars. However, why is the $_GET variable not seen in my echoed alert when I submit a form as in the following simplified code?
//video.php?id page
<?php session_start();
include 'connect.php';
$Vid = mysqli_real_escape_string($_GET['id']);
$login_id = mysqli_real_escape_string($_SESSION['login_id']);
if (isset($_POST['sample'])) {
echo "<script>
alert('$Vid');
</script>";
}
else//etc.
?>
<html><head></head><body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" id="Form">
<button name="button" type="submit">Click</button>
<input type="hidden" name="hidden" value="sample">
</form>
</body></html>
When I alert $Vid, nothing is alerted (blank alert box). Obviously, I see the SESSION variable when I alert $login_id. Am I missing something with the $_GET? Is there any way for the global var $Vid to be recognized? If I could use $Vid it would save me 5 or 6 queries based on how my code is currently written.
This how you can correct
Put $row['id'] inside a hidden text box with name as "id"
Your form method is POST, so use $_POST to grab the data in the POST file.
I think you escaped wrongly and thus the id is not appended (notice the backslash after $row['id']?), try the following:
echo '<form action="video.php?id='.$row['id'] . '" method="get" target="_top">
<input type="image" src="' . $image . '" style="width:180px;height:120px"
alt="Submit"></form>";
Imho your coding style is very unreadable with all those backslashes. It's okay to mix single/double quotes where needed…
[edit] and you obviously need to change the method to "get". ;-)
Changed action from $_SERVER['PHP_SELF'] to action="" and now the GET variables are seen.

php form not working with hidden field

Not sure but this may be a bug in php.
Here is my dummy code.
<script language="javascript">
function fb_logout() {
document.getElementById("hidden_logout_id").value
alert(document.getElementById("hidden_logout_id").value);
document.getElementById("form_1").submit();
}
</script>
echo "<form id=\"form_1\" action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">";
echo "<input type=\"hidden\" id=\"hidden_logout_id\" name=\"hidden_logout_name\" value=\"1\"/>";
echo "<input type=\"text\" id=\"h_logout2\" name=\"h_logout2\" value=\"1\"/>";
echo "</form>";
var_dump($_POST);
echo "<span onclick=\"fb_logout();\">Logout</span>";
When doing a post trough the click on the span the values of the hidden and the text inside the form do not get posted: var_dump($_POST) shows an empty variable. But it's strange that if I remove the hidden from the form (or I just place it outside the form) it works and it passes in the $_POST the value of the input text remaining in the form.
Does this has to do with that the hidden is modified from an event outside the form?
Any help is appreciated.
This code may help..
<?php
if(isset($_POST['hidden_logout_name'])) var_dump($_POST);
?>
<script language="javascript">
function fb_logout() {
alert(document.getElementById("hidden_logout_id").value);
document.getElementById("hidden_logout_id").value = "hidden_logout_value";
document.getElementById("form_1").submit();
}
</script>
<form id="form_1" action="" method="post">
<input type="hidden" id="hidden_logout_id" name="hidden_logout_name" value=""/>
<input type="text" id="h_logout2" name="h_logout2" value="1"/>
</form>
<button onclick="fb_logout();">Logout</button>
document.getElementById("hidden_logout_id").value
you have not done any action for this line either assign value to this or assign its value to variable.
var logout_value=document.getElementById("hidden_logout_id");
alert(logout_value.value);
document.getElementById("form_1").submit();
May this help you.
look for the
hidden_logout_name
instead of
hidden_logout_id
so your value is in the
$_POST['hidden_logout_name']

How can I pass the value of a hidden text field to another php page?

I have a php page with the following:
echo "<form method='post'><input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . "></form>";
Now I created another php page and want to grab the value of this hidden field and place it into another variable. How can I do this? I tried using this for my second php page:
$member = $_POST['memberIdd'];
But I just keep getting "undefined" for $member.
Thanks
I think your problem is that you did not set the right action in the form and you also should have a submit button. Thus, your form should look like:
echo '<form action="url-to-the-second-page.php" method="post">';
echo '<input type="hidden" id="memberIdd" name="memberIdd" value="' . $memberId . '">';
echo '<input type="submit" name="submit" value="submit" />';
echo '</form>';
<form method='post' action='secondpage.php'>
http://www.w3.org/TR/html4/interact/forms.html#h-17.1
If you don't write the action then the posted data goes to the same page where you were.
And by the way, the variable $memberId (the one in your question) at the firstpage.php should be defined. In other case it will prompt error message.
Edit: It will better for you to use HTML codes out of PHP.
That's a hidden form field, but you've no submit button. The value won't appear in the second page unless you specify it as the form's action (i.e. where the form should be submitted to), and also submit the form. e.g.
echo "<form action='page2.php' method='post'>
<input type='hidden' id='memberIdd' name='memberIdd' value=" . $memberId . ">
<input type="submit" value="Go to page 2">
</form>";
Alternatively, you could store the value as a session variable.

Categories