how can I put a variable in session only if link is clicked?
Note: My link have to submit a form at the same time.
If you're using someting like this in your HTML to submit your form
<input type="submit" name="sender" value="Send" />
you can check the $_POST['sender'] Variable. It will output ether Send (clicked on button) or nothing (clicked on link).
You can make a hidden input inside the form
<input type="hidden" id="link_is_clicked" name="link_is_clicked" value="0"/>
then when clicking the link, change the value of that input
<a href="..." onclick="document.getElementById('link_is_clicked').value = 1" ...
then check for the value of the element on the server side.
Just check whether your form is submitted or not, if so set the session variable.
For example:
if(isset($_POST) && count($_POST) > 0)) //Assuming your form's action is post, use GET otherwise
{
$_SESSION['somevar'] = 'somevalue';
}
I assume you're using JavaScript to make the link submit the form? You can add a hidden input that has value 0. When the user clicks on the link, change the value of the input to 1 before submitting.
Related
I have a simple form that collects data and sends it to a PHP script using POST.
<form method="post">
<input type="text" name="cost">
<button name="submit" type="submit">Submit</button>
</form>
The PHP script is,
if(isset($_POST['submit'])){
echo "set";
}
I want to know what happens when I click on the submit button?
The PHP manual says the following about isset,
isset — Determine if a variable is set and is not NULL
When exactly is the submit button SET? When I echo out echo $_POST['submit']; it outputs nothing.
It's only when I use the value attribute along with the submit button that I get something on $_POST['submit'];. Why should I use the value with the submit button? What exactly does it do?
I want to know what happens when I click on the submit button?
It submits the form.
When exactly is the submit button SET?
When the user submit's the form.
When I echo out echo $_POST['submit']; it outputs nothing.
You didn't specified a value for it, so it returns an empty string ($_POST['submit'] === "")
Why should I use the value with the submit button? What exactly does it do?
Well on an button the value is not needed, it is enough when it is set, so you can check if the button was submitted and not an other form f.ex.
Try with this
<form action="" method="post">
<input type="text" name="cost" />
<input type="submit" name="submit" value="Submit" />
</form>
In php side
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
echo "set";
}
It submits the whole form data into targeted location and the GET and POST methods are used to send encoded data to the targeted location
The GET method is restricted to send upto 1024 characters only.
The POST method does not have any restriction on data size to be sent.
I have form with a submit button that changes value depending on the page. When the submit button is pressed, I need that value to do some data validation. However, the value is being lost somewhere in the process.
I did a value check on the button using 'click' in jQuery that fires before the 'submit' of the data. The value is correct.
On the page load, I tried checking the POST variable value and it is gone.
EXCEPT in the following situations:
If I do NOT use the POST variable in any way, such as 'if' statements. Or assign its value to a variable. If I do any of those things, the value is lost does not even show up when I check for its value at the top of the page.
2.If I leave one of the required fields in the form blank.
I should note that this is a Wordpress site. I know people are going to be asking for code, but the page is quite long, so I will try to get some code here soon.
Submit buttons do not submit their values, add an input type='hidden' with the value
Add an input hidden and set its value to the value of the submit button right before pressing the submit button:
$(document).on('submit', '#form', function(e) {
e.preventDefault();
$('#form input[name="submit_value"]').val($(this).find('input:submit').val());
$(this).submit();
});
HTML:
<form method="post">
<input type="hidden" name="submit_value" />
<input type="submit" value="Submit" />
</form>
I finally found the error. Turns out it didn't have to do with the POST.
There was an extra line in the code that kept resetting a variable back to the initial value.
Sorry for the trouble and thanks for the help anyway!!
if(isset($_POST['submit'])){
//code here
}
Correct me if I am wrong, but I believe the code above will not work if the user submits the form using the "enter" key.
Is this true? If so, is there another if statement I can use instead of this to cover both the user using the submit button and pressing the enter key?
To check if a form is posted to the server use:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// …
}
If you are not sure if submit will be in POST you can use overkill method: create hidden <input> in this <form> with some unique name and verify if it in POST data:
<input type="hidden" name="some_name" value="OK" />
And in PHP code:
if (isset($_POST['some_name']) && $_POST['some_name'] == "OK") {
// ...
}
This is not true, if submit is the default submit button.
Also, there's a $_SERVER['REQUEST_METHOD'] — I take it it would be POST when you submit your form or GET otherwise, if I understand your intention right.
I'll expand on the other answers: it's probably not true if submit is input type=submit and the enter key triggers that button. If it's type=img or a <button>, the behavior may be different with the enter key (and may not set that _POST value).
The submit should be fine if it is the only submit button on the page or the first submit button on the page if there is more than one, which is probably unlikely.
An alternative would be to use a hidden input.
e.g.
<input type="hidden" id="processed" name="processed" value="1" />
then in your submit script
if(isset($_POST['processed'])){
//code here
}
Ok I have a form with multiple submit buttons.
The coding on my php file has a header with a url depending on which form was entered. My issue is when I submit the form( no matter which button I use) the window that pops up is not the url action assigned to that button but the php file itself. What am I doing wrong?
the form starts of like this so that you can see if I directed it correctly
<form method="post" action="http://gamerzacademy.com/foodCYO.php" target="_blank">
<input type="text" name="uid">
<input type="submit" name="Dish1" value="Dish1" onclick="
this.disabled=true;
this.value='Gift Opened';
document.FreeFoodForm.submit();">
<input type="submit" name="Dish2" value="Dish2" onclick="
this.disabled=true;
this.value='Gift Opened';
document.FreeFoodForm.submit();">
etc......
now the php file starts like this
<?php
if ($_REQUEST['Dish1'] == "Dish1") {
header("Location: url1".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish2'] == "Dish2") {
header("Location: url2".urlencode($_POST['uid']));
}
else if ($_REQUEST['Dish3'] == "Dish3") {
header("Location: url3".urlencode($_POST['uid']));
}
.....etc
?>
You are posting the form through Javascript. The code doesn't know which button was clicked, so the value of that button isn't posted to the form. Therefor, your form cannot see which button was clicked. If you change the method to get, you will see which value do or do not get posted.
I think you don't need to post from Javascript at all. Just let the button do the posting. Only the name and value of the button that was clicked will be posted.
B.t.w., you disable the button, presumably because you don't want people to press the button twice, but in your setup they still can press any other button. I think it is wise to disable all of them.
Two things:
First, make sure the URLs you are sending in header are valid URLs.
Second, it looks like you have some whitespace before the <?php opening tag. Make sure there is no whitespace before the PHP opening tag. If there is, header won't work.
By doing the following in JavaScript:
this.disabled = true;
You effectively don't send its value to PHP.
A better idea might be an on submit handler in the form that prevents double submit.
I have this conventional submit button which submit a form like this:
<form method="post" id="form_submit">
...
<input class="button" type="submit" name="Submit" value="Submit">
</form>
And I check if the submit button is clicked using this:
if(isset($_POST['Submit'])){
//update DB
}
Now I have a submit link using jquery:
Submit
JS code:
$("#form_submit").submit();
What is the alternative way here to be used here for if(isset($_POST['Submit'])) since I'm submitting the form using javascript?
If I understand you correctly, try this:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// your code.........
}
You should add a hidden input <input type="hidden" name="formsubmit" value="yes" /> to the form which will always get submitted, and check for that instead of the button (which only gets submitted if it is clicked on ..)
If I understood your problem correctly that you can simply change input type to hidden.
<form method="post" id="form_submit">
...
<input type="hidden" name="Submit">
</form>
$_POST['Submit'] variable will be defined.
The best solution is "Don't do that". If you want to submit a form then use a submit button (don't do it as a side effect of clicking on a hyperlink to the top of the page). Any JavaScript you want to run can then be handled in the form's submit event.
If you really want to do it as a side effect, then check for the existence of any other field that you know will be set. You could add a hidden field to ensure there will be one of a given name/value combination if you like.