I want to keep the value from a input box even when the page is refreshed.
I know that i could do that using just an echo, but i'm connecting two different files.
I have one page with the form:
<form method="POST" action="mypage.php">
<input type="hidden" name="value" value="test">
<input type"submit">
</form>
And another one(mypage.php) that reads the form
<input type="text" name="value" value='<?php echo $_POST['value']; ?>' />
The code is working, bue lets imagine that someone refreshed the page and the value desappears.
I want to know how can I keep the value inside the textbox even when someone refreshes the page.
Thanks.
Create a session to preserve the value:
<?php
session_start();
if((isset($_SESSION["preserve"]))&&($_SESSION["preserve"] != "")){
$_SESSION["preserve"] = $_POST['value'];
}else{
$_SESSION["preserve"] = "";
}
?>
<input type="text" name="value" value='<?php echo $_SESSION["preserve"]; ?>' />
<?php
//when you don't need this session anymore do this:
//(that can also be called in another page of your project)
unset($_SESSION["preserve"]);
?>
Hope that's what you were looking for...
Related
I am wondering if the folowing is possible.
I fetch a list of names as an array from a SQL database. I need all those names to be posted with a html form one by one. This action should be activated with one button. When the button is clicked the names should be posted one by one untill all names are posted, then stop. So probebly jquery or javascript is needed but that is new for me. I have been searching but I can not find anything that can help me accomplisch this.
I am sorry for asking this question and my language (english is not my main language) but I don't know if this is even possible and I cant find any corresponding topics while researching..
PS: I can not use Ajax for the post !!
Example to get the names:
$stmt = $mysqli->prepare("SELECT username FROM example WHERE examplefield = 1");
$stmt->execute();
$result = $stmt->get_result(); //only works when nd_mysli is set on the server!
while ($rowid = $result->fetch_assoc())
{
$arrayusername[] = $rowid['username'];
}
I need all the names from the $arrayusername[] to be posted with below form one by one by pressing the following button
<input type="button" value="Post all names one by one"
onClick="sendallvalues('???') "class="example_c" />
// The button should do the following post name 1, end. post name 2 end, post name 3 end. stop script when all names are posted.
<form method="post" target="_example" action="https://www.example.nl">
<input type="hidden" value="<?= $arrayusername[] ?>" name="postvalue" >
</form>
// should be hidden to the user and is only ment for the name atrribute to have a place ! All the stuff needs to happen by pressing that one button !
<script>
function sendallvalues(???) {
//I have no Idea where to begin to make this happen.. But it should post the form one by one with one value at the time untill all names are posted.
}
</script>
Maybe you can explain more detailed what you need.
The value attribute should have one name and you create different input fields.
<form method="post" target="_example" action="https://www.example.nl">
<input type="hidden" value="<?= $arrayusername[] ?>" name="postvalue" >
<input class="example_s" type="submit" value="Post name">
</form>
should be
<?php
$arrayusername = array('Name1','Name2');
echo '<form method="post" target="_example" action="https://www.example.nl">';
foreach ($arrayusername as $key => $value) {
echo '<input type="hidden" value="' . $value .'" name="name-' . $key . '" >';
}
echo'<input class="example_s" type="submit" value="Post name">';
echo '</form>'
?>
Basically I've got a form with 5 radio buttons. No submit button. I want the form to run when the user clicks on any of the radio buttons, so this is what I did.
<input id="5" type="radio" name="star" onchange="this.form.submit();" <?php if ($row["star"] =="5") echo "checked";?> value="5"/>
a querystring is required for the form so I'm using a form action like this
<form name="submit" action="http://example.com/page.php?id=<?php echo $urlid;?>&title=<?php echo $title;?>" method="POST">
and my php is
if ($_POST["submit"]) {
$rating = $_POST['star'];
$title = $_GET['title'];
$verification = ($_GET['verification']);
} else {
//run the page like usual
}
After testing, I found that onclick, it runs the form action, but on the php side, it goes to "else" where is runs the page like usual instead. Any ideas?
Your PHP is checking if $_POST['submit'] contains a value. Your form does not contain a form element with the attribute name="submit", so therefore it fails and moves straight to the else statement.
If you want to check if the form was posted then you should instead check for:
if (!empty($_POST)) {}
or
if ($_SERVER['REQUEST_METHOD'] == 'POST') {}
The form element seems to have invalid attributes, missing a quote and space.
It's generally easier to write a little more code, and keep it clearer
<?php
$url = "http://example.com/page.php?id=". $urlid ."&title=". $title;
?>
<form name="submit" action="<?php echo $url; ?>" method="POST">
Since you are checking with -
if ($_POST["submit"]) { // this checks whether there is any item named 'submit' inside the POST or not
} else {
//run the page like usual
}
The easiest would be to put a hidden item with name submit so that the check validates to true-
<form .. >
....
<input type='hidden' name='submit' value='submit' />
</form>
I've got a working PHP form that I've built and it contains 5 fields which are pre-populated by a URL query string.
The query string URL is on a hyperlink on an HTML email asking the recipient to click here to make a booking. I've got their details already because I've sent them the email, hence why the form get pre-populated.
How can I get the form to instantly submit on page load and redirect to the thank you page?
Here's how my form looks at the moment:-
<form name="frm" id="frm" method="POST">
<input name="firstname" id="firstname" type="text" value="<?php echo ((isset($_GET["firstname"]))?htmlspecialchars($_GET["firstname"]):""); ?>" />
<input name="lastname" id="lastname" type="text" value="<?php echo ((isset($_GET["lastname"]))?htmlspecialchars($_GET["lastname"]):""); ?>" />
<input name="email" id="email" type="text" value="<?php echo ((isset($_GET["email"]))?htmlspecialchars($_GET["email"]):""); ?>" />
<input name="company" id="company" type="text" value="<?php echo ((isset($_GET["company"]))?htmlspecialchars($_GET["company"]):""); ?>" />
<input name="contactid" id="contactid" type="text" value="<?php echo ((isset($_GET["contactid"]))?htmlspecialchars($_GET["contactid"]):""); ?>" />
<input type="submit" name="Submit" value="submit" />
</form>
And here's the JavaScript I've tried (but didn't work):-
window.onload = function() {
setInterval(function(){
document.getElementById("frm").Submit();
},5000);
}
Here's a pastebin of the whole page: http://pastie.org/private/lgpealjya8xrwqi78gropw
Thanks
Alright so basicaly you have been trying to get information through url, insert them in a form and then submit the form so you can send an email. This is all quite nice although you're just making things harder than they are.
You you need to do is wipe off the form (we don't need this really) and instead of processing all those $_POST information you wanted to get off the form, you gonna do that with the $_GET array you already got off the url, at the end of the day it's exactly the same since the informations you're inserting in the form are only the ones you're getting off the url.
So if you change this the following way, youe script will work just fine :
//Get the submitted data
$firstname = cleanText($_GET['firstname']);
$lastname = cleanText($_GET['lastname']);
$email = cleanText($_GET['email']);
$company = cleanText($_GET['company']);
$contactid = cleanText($_POST['contactid']);
So here is what's gonna happen : the user is going to clic on a link, your script is going to get the $_GET array, process it, send the email based on this array, and this send the user to the thankyou.php page. Note that you could get your script to be more efficient and save the user some time if you insert your script in the thankyou.php.
Last but not least I would like to point out to you that it's not very good practice to get an email sent on url submission... you could end up having a bot send massive amounts of emails through this url and get you email blacklisted. If I were you I'd either control the ip and make sure only 1 mail is sent every 10 minutes or whatever time suits your needs best, or i'd add a captcha to the page so the user would need to show he's a human being who actualy wants to waste some time sending email.
try using: document.getElementById('frm').submit(); instead of document.getElementById('frm1').submit();
and also make it execute once the page has been loaded:
window.onload=function(){
document.getElementById('frm').submit();
}
I'm trying to grab a session variable in my javascript, but have some problems getting it right..
I didn't get that to work, so instead i tried to store it in a hidden variable in HTML first, but i dont know how to store a session variable there..
Can anyone guide me to the right path?
The code (HTML file): ($_SESSION["price1"] is equal to "20.00")
<input type="hidden" id="price" value="$_SESSION["price"]"/>
The code in javascript:
var sessionValue = document.getElementById("price").value;
What to do?
you missed PHP tags <?php and echo, change to:
<input type="hidden" id="price" value="<?php echo $_SESSION["price"]; ?>"/>
What everyone bellow mentioned is perfectly true, inside HTML you can just wrap stuff in <?php echo $_SESSION['price']; ?> and it will work (notice the <?php tags)
However, the right way to do this -- considering you want to pass a PHP variable (in your case a session variable) to javascript is to directly insert it into a javascript variable instead of accessing the DOM. This way, you also have it instantly and don't have to wait for the DOM to be loaded
<script>
var price = '<?php echo $_SESSION["price"]; ?>';
alert('The price is: ' + price);
</script>
This line:
<input type="hidden" id="price" value="$_SESSION["price"]"/>
Should be:
<input type="hidden" id="price" value="<?php echo $_SESSION["price"];?>"/>
You are missing the php tags
change,
<input type="hidden" id="price" value="$_SESSION["price"]"/>
To,
<input type="hidden" id="price" value='<?php echo $_SESSION["price"]; ?>'/>
Grab your session variable in JavaScript using this:
var my_session_price = "<?php echo $_SESSION["price"]; ?>";
I have this query in php code:
$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
I have echoed the vName.
echo $row['vName']."\n";
The above values appear in a autocomplete textbox.
In the same aboce echo statement I want to pass 'id' as hiden value. is it possible? I wan to retrieve it in another page.How do I do this?
$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
I hope you get the idea.
Echo it out to a hidden input field,
<input type="hidden" name="hidden_id" id="my_hidden_id" value="<?php echo $row['id'];?>"/>
This way you can pass it with a form if you are submitting one or simply add an id to it and get the value to pass to the next page.
I would first make a variable of $row['id'] and when you're still in your form but outside the textbox do this:
<?php echo "<input type='hidden' name='id' value='".$yourvariable."'/>";?>
If it must be completely hidden i recommend using php-sessions.
I guess you want to do that if you want to retrieve the id in other page , you better do this :
<?php echo $row['vname'] ?>