php form not working with hidden field - php

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']

Related

Why there's no output generated?

What wrong with my code? uniqid() is for generating a unique code and it will be stored to the name attribute then a condition is stated if it is clicked it will generate it's working.. Could some help me please with this one? Thanks in advance..
<html>
<form method="POST">
<?php
$attname = uniqid();
echo "<input type='submit' name='$attname' class='btn btn-success' value='Post to PeĆ³nline'/>";
echo $attname;
if(isset($_POST[$attname])){
echo 'its working';
}
?>
</form>
<html>
This isn't going to work.
When you refresh the page the $attname value will change. This will happen when you submit the form. So the actual name you're checking on will change and not be the same as the new $attname.
Put the following after your echo $attname; line:
print_r($_POST);
Also for this to work properly you'll need to nest the <input> tag in a <form> tag e.g.:
<form method="POST">
<input>...</input>
</form>
The $attname will change after page refresh.
You need to store that name somewhere.
Add another hidden element.
...
echo "<input type='hidden' name='elemname' value='<?php echo $attname;?>'/>";
...
And after submit,
if (isset($_POST['elemname'])) {
$elemname = $_POST['elemname'];
$val = isset($_POST[$elemname]) ? $_POST[$elemname] : '';
echo $val;
}

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

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.

this.form.submit() with PHP isset?

echo "<form method=\"post\" action=\"settings.php\" onchange=\"this.form.submit()\">";
echo "Announce New Files: <input type=\"radio\" name=\"announcefiles\" value=\"On\" $checkon1> On";
echo "<input type=\"radio\" name=\"announcefiles\" value=\"Off\" $checkoff1> Off<br>";
echo "</form>";
I am trying to get this form to submit when one of the radio buttons is pressed but I'm not sure how to catch the submission.
for example, normally with a submit button you would use something along the lines of if(isset($_POST['submit'])) but I'm not sure how to do it if the form auto submits.
Add hidden input field like:
<input type="hidden" name="action" value="submit" />
Then in PHP check:
if(isset($_POST["action"]) && $_POST["action"] == "submit") { ... }
You should be checking the request method. If you've set things up cleanly, a POST request at that URL will mean a form submit. As you've noticed, you can have attempted submits where a value just isn't there.
if ($_SERVER['REQUEST_METHOD'] === 'POST')
See $_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST' for more discussion.
Give your form a name and check for isset($_POST['form_name']) or check for the name of the radio isset($_POST['announcefiles'])
Also, you don't need all the quote escaping that you have, you can use single quotes as well as use a multiline string - see example below.
echo "
<form method='post' name='form_name' action='settings.php' onchange='this.form.submit()'>
Announce New Files: <input type='radio' name='announcefiles' value='On' $checkon1> On
<input type='radio' name='announcefiles' value='Off' $checkoff1> Off<br>
</form>";
<?php
// Check if form was submitted
if (isset($_POST['form_name']) {
// Form submitted
}
?>
<?php
// Check if radio was selected
if (isset($_POST['announcefiles']) {
// Form submitted
echo 'You chose' . $_POST['announcefiles'];
}
?>
Try this:
You may have an easier time if you separate the php and html a little more.
<form method="post" action="settings.php" onchange="this.form.submit()">
<fieldset>
<legend>Announce New Files:</legend>
<label for="on"><input type="radio" id="on" name="announcefiles" value="On" <?php echo $checkon1 ?> /> On</label>
<label for="off"><input type="radio" id="off" name="announcefiles" value="Off" <?php echo $checkoff1 ?> /> Off</label>
</fieldset>
</form>
Then in your php logic in settings.php ( or above the form if you are posting back to the same page ) you can check for the value of announcefiles:
<?php
if(isset($_POST['announcefiles'])){
// DO SOMETHING
}
?>
Let me know if this helps. Or if I'm missing the question.

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.

post an array to webpage

it has error because $_POST['sub1'] can't be accessed
is there any approach or solution to echo the value of $_POST['sub1']? or impossible? no way? even with another arrays?
i had question about my code nobody solved it! then I decide to tell it in simple way.
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
I think I know what is wrong here.
When you submit the form the second time (for sub2) you are no longer posting the value of sub1 along with it, just sub2.
This should fix it:
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='hidden' name='sub1' value='" . htmlentities($_POST['sub1']) . "'>";
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
You're using submit buttons. Only the button that you actually click will have its name/value pair sent to the server. When you click the sub2 button, only sub2=sub2 is sent, so sub1 won't exist in the $_POST array.
followup:
$_POST is created for you by PHP based on what's sent from the browser. The way you've built your form makes it impossible for 'sub1' to exist when you click the 'sub2' button. In other words, you need to use the SAME name= for BOTH buttons, and change the value= as appropriate:
html:
<input type="submit" name="submit" value="sub1" />
<input type="submit" name="submit" value="sub2" />
php:
if (isset($_POST['submit'])) {
echo "You clicked the {$_POST['submit']} button";
}

Categories