I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:
The user will click 'Exit Group'.
<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>
The code that should be execute when 'Exit Group' is clicked is below:
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.
If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.
You use this kind of link normally to jump to an element inside the page (e.g. Top to jump to an element with ID top). This is completely handled in the browser.
And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.
You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:
<form action="" method="POST">
<input type="submit" name="logout" value="Exit Group" />
</form>
Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.
You could do it also with the link, but with a GET request this way:
<a id="exit" href="?logout=1">Exit Group</a>
<!-- ^-- parameter must be part of the URL, name has no effect -->
and
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
Note that you have to pass a parameter logout it here.
It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:
<form method="POST">
<input type="text" name="foo" value="" />
<input type="text" name="bar" value="" />
<input type="submit" name="send" value="Send" />
</form>
if the user clicks on the submit button, the $_POST array at the server side will have the keys:
$_POST['foo']
$_POST['bar']
$_POST['send']
This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:
Link
will result in
$_GET['foo']
$_GET['bar']
$_GET['andMore']
You probably should read about the HTTP protocol.
a isnt a form control. it needs to be an input or select if it's within a form.
For manual linking, do href="/page?logout"
You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.
edit: added simple example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Form test</title>
</head>
<body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
<pre><? print_r($_POST)?></pre>
<?endif;?>
<? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
<? // we'll end up back in this file when the form is submitted. ?>
<form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="textbox"
value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
<input type="submit" name="submitbutton" value="Submit" />
</form>
</body>
</html>
$_POST will only be filled if you use a form with method=post.
Yes. A POST and a GET are two different things ;)
if(isset($_GET['logout']))
This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.
Then logoff will be in the $_GET array.
Related
I am trying to pass a GET parameter through a button but I can't figure out what I am doing wrong. The parameter is set, as it shows up fine in the header, but it isn't being added to the edit.php url. The button is directing me to edit.php, just without the GET parameter added. I am pretty new to this stuff and this is my first time using links that aren't through anchor tags, so I am clearly missing something here. Any advice is greatly appreciated.
<h1 class="headerWithButton">Claim #<?echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
</h1>
When you submit a form using the GET method, any existing query string in the action will be replaced by a new one generated by the name and value of the successful controls associated with that form.
In your case, the only successful control is the submit button, which doesn't have a name or a value.
You could get the effect you desire by moving the data to those attributes:
<h1 class="headerWithButton">Claim #<?php echo htmlspecialchars($_GET['claim_id']); ?>
<form>
<button formaction="index.php" class="backButton">Back</button>
<button formaction="edit.php" name="claim_id" value="<?php echo htmlspecialchars($_GET['claim_id']); ?>" class="editButton">Edit</button>
</form>
</h1>
Important security note: inserting data from the URL directly into a page makes you highly vulnerable to XSS attacks. You need to take precautions against that. The most basic of those is using htmlspecialchars.
Note, however, that it isn't really appropriate to use a form here. Your form buttons are not submitting any data the user has entered, nor performing any kind of action. The affordances offered by buttons are misleading here.
You can, and should, use regular links instead.
<form method="get" action="edit.php">
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
instead of using the form you can just use a straightforward link
k with the anchor tag
edit
or you can specify the methos of get on the form with a hidden for input to place the link get parameter
If you have to use formaction, you must specify name and value of element:
<h1 class="headerWithButton">Claim #<? echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?php echo('<button type="submit" formaction="/edit.php" name="claim_id" value="'.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
here it is better to place buttons in different blocks. But personally, in this case, I use a hyperlink
<form method="get" action=""></form>
<?echo('Edit
I find the easiest way to pass values with a button is to just wrap a form around the button.
$id = $_GET['claim_id'];
echo <<<EOT
<form action="index.php" method="get">
<button>Back</button>
</form>
<form action="edit.php" method="get">
<button name="claim_id" value="$id">Edit</button>
</form>
EOT;
I have my page as follows:
<form id="filterQuery" action="#" method="post">
...
<input type="submit">
</form>
<?php
// use $_POST to determine query restrictions and conditions
?>
Now my problem is with the action of the form, because if I make it "#" self, nothing happens when clicking submit.
I'm using a template which shows subpages as include in <div>s. In other words, my page is not a standard HTML page which contain <head> and <body> etc, but is in the following format: basic.php#!/my_sub_page.
Trying to make action "basic.php#!/my_sub_page" results in an empty _POST
I tried your example and everything works fine with /index.php#!/smth in action and post variables.
<form action="#!/something" method="post">
<input type="text" name="var1" value="123">
<input type="submit"></form>
<?php
print_r($_POST);
?>
This is how I check it.
Are you sure you didn't forget name attributes in inputs?
Do you see their values in url after ? if you change method to get?
<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example
Here is the form that is inside index.php -- I'm trying to programmatically submit it using a click on an image that's nested inside an 'a' anchor:
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript:document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
Here is my php code:
<?php
if(isset( $_POST['imageFormSubmitter']))
{
// this fx just echos a javascript 'alert()'
showAlertBox("The index page form 'landingForm' was just submitted.");
}
else
{
var_dump($_GET);
var_dump($_POST);
}
?>
When I type something in the edit box on the form, then click on the image 'bronzeAgeCaveDrawing.jpg' the dump of the GET and POST arrays says:
array
Empty
array
Empty
And for that matter I'm not even sure the above PHP code is getting called for the
<a href="javascript:document.landingForm.submit()">
case at all because the above php code executes regardless of whether the form submits -- the GET and POST
dumps occur when index.php loads due to the 'else' clause above that I added because it didn't appear that the 'if' clause was happening.
If the above php code IS in fact executing by the submit() call, the GET and POST are still empty arrays.
I studied examples of using an anchor 'a' with an image combined with a call to submit() in a few posts on SO and I don't see any reason why the POST or GET array is empty. And due to my form method="post" I'm 100% expecting for the submit() to POST the form, not submit with a GET.
Been staring at this awhile -- am I missing something?
UPDATE: Here is the functioning code -- thanks for your help folks!
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250" name="itemName_name"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript: document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
if(isset( $_POST['itemName_name']))
{
showAlertBox("The index page form 'landingForm' was just submitted.");
var_dump($_POST);
}
Output of the dump of POST array NOW shows:
array
'itemName_name' => string 'fffffffffff' (length=11)
TAKEAWAYS:
1) the syntax "document.landingForm.submit()" is 100% fine.
2) the 'name' attribute of my <img> tag does NOT get posted (not sure why,
but it's not the worst thing happening for me right now so I'll deal with it)
3) giving the text <input> field a 'name' attribute made its way fine to the POST array.
4) the call to submit() does in fact pay attention to the method="post" in my
form declaration.
I'm running with it, thanks all. There is more than one equally acceptable answer for me, so I'll +1 y'all
then have to choose one. Thanks again.
You're not setting the name attribute on your <input> tag, and the <img> tag is not the right way to submit with an image.
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250" name="artifactName"
id="artifactName" />Enter an artifact name here...
<input type="image" src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</form>
Source: HTML Dog tag reference
Note also that the <input> tag in HTML5 does not / cannot have children.
After I add a button and a text field, how can I program that button to simply take what's in the text box and put it into a variable? I have no idea how the button click event works.
<form id="form1" name="form1" method="post" action="">
<label>
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="xxxxxxxxx " />
</label>
<label>
<input type="text" name="txtboxsearchbydistro" id="txtboxsearchbydistro" />
</label>
</form>
Would I put a PHP statement in the space where the xxxxxxxx is at?
Any help would be great!
You can't execute PHP code in onclick() statements because PHP gets executed on the server, before the page is sent to the browser, and the onclick() function is exectued at the browser.
Solution would be (assuming this page is form.php) set the action of the form for "form.php" and on that page have
if(isset($_POST)){
$variable = $_POST['txtboxsearchbydistro'];
// Here you can run validation on $variable, sanitize it and pass it to a DB query
}
No, php is server-side, and onClick is client-side event.
I'm not entirely sure what are you trying to accomplish. If you wish to submit your txtboxsearchbydistro value to some PHP script, you would put something like this:
<form id="form1" name="form1" method="post" action="somePhpScript.php">
Then you would use something like Bobby proposed.
If you wish to do something before you actually send the form, or you want to do something on client side (i.e. in visitor's browser), you'd need to do something like
<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="myScript();" />
You could then need to define your script, and assign your value there.
Hope it helps.