if (isset($_GET['editpersonnel'])) not working - php

I am working on PHP/MySQL project with a modest CMS.
I am working on a page called - index.php?page=personnel - where I am trying to update a personnel in the database.
On that page I have an included form with - action="?editpersonnel" method="post"
Meanwhile, in PHP:
if (isset($_GET['editpersonnel'])) {
... // update personell ...
header('Location: .');
exit();
... idea being that when the form is submitted the script updates the personnel and then redirects back to itself.
Unfortunately, when I submit the form, instead of reloading the desired 'index.php?page=personnel' the script simply ads 'editpersonnel' to index.php so I end up with
index.php?editpersonnel
And by the way, none of the updates happen either...
Actually, it completely disregards everything after - if (isset($_GET['editpersonnel'])) - so I guess that's were the problem is...
Any ideas what might be causing such a behavior?
p.s.
Form is dynamically populated, so this is its page source:
Name:
Email:
Set password:

You need to use $_GET to be able to use something like that. Change your form's method to GET.
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="text" name="editpersonnel" />
<input type="submit" value="submit">
</form>
Something like that should work.

You are passing the form data by POST method and then trying to access them via GET
You have two possible solutions, see which one is more suitable for your case.
Change form method to GET
<form action="?editpersonnel" method="GET">
OR
Change access method in PHP to POST
if (isset($_POST['editpersonnel'])) {
... // update personell ...
header('Location: .');
exit();
}

Related

PHP submit blank screen - script not running on POST?

I have a script that pulls an XML page and uses a form to update and save the values back. When I click the submit button it works, but then the page loads blank. I just want the page to refresh. There are about 100 different threads on this, and nothing I have tried has worked to resolve the issue. Out of curiosity, I just tried to run the window.location script and nothing else, and this piece actually doesn't work at all.
<?php
//if (isset($_POST['submit'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$ctstatus->nodeValue = $_POST['ctstatusform'];
//htmlentities($xml->save('test.xml'));
echo '<script type="text/javascript">window.location="http://google.ca";</script>';
}
?>
The inner contents of the form don't really matter at this point, I just want it to refresh the page after I hit the submit button.
I previously used isset but from reading it seems like that's obsolete, and my form action="" used to be blank. Either way my XML save works, but nothing to refresh the page. I also tried header and that didn't work either.
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input class="save" name="submit" type="submit" value="Save" />
</form>
Out of curiosity I tried an onClick function with a timer and this does work but it's not ideal at all, especially because the page could technically refresh before the POST is finished writing the file. I'd rather know why the echo doesn't execute.
PHP redirect would most likely be preferable to JavaScript redirect.
Typical structure when posting back to same page:
<?php // cannot be any output before this (space, linefeed, etc)
if(isset($_POST['submit']) {
// do stuff with the submission
header('Location: http://google.ca');
exit;
}
// does your script need to do some other data retrieval or calculation? do it here.
?>
<html>
... snip ...
<form method="post">
... snip ...
<input class="save" name="submit" type="submit" value="Save" />
</form>
Following this simple structure for procedural scripts--
Deal with user input / redirect
Do logic (collect, manipulate data)
Show output (using php only to insert variables and looping)
will help you avoid a lot of heartache and technical debt.
Okay, I have gotten this sorted out. It turns out that the problem was embarrassingly simple, but maybe will assist someone in the future. Along with reordering my code, as Tim suggested. I specified HTML as the DOCTYPE, and that worked to resolve the issue. I no longer need to worry about refreshing the page after submit, because it refreshes as it should automatically. Thank you to everyone who commented.

WordPress Custom Page Submit Form

I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.
I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>
this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have
if(isset($_POST['Submit']))
But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php
Btw, this custom php is on a page with such url domain.com/custompage/
How can make this form work ?
ps. Code above is for reading from mysql.
You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".
You need to make those match.
Either by changing the name of your submit button to name="Submit"
or by changing your conditional statement to if(isset($_POST['shaSubmit']))
which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".
You need to change 2 things.
(1)make action=""
don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"
(2)if(isset($_POST['shaSubmit'])){
the code ....
}

HTML and PHP in one file

I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document

How to redirect page using $_SERVER['REQUEST_URI']

I'm building a page that has a form that's submitting to my database. The form needs to post to the current page to process, but since I'm using this same form on many pages, I would like a way to tell the form to use the user's current page to submit to. I know the code below can get this information, it's what I'm using to send url data to my database for another use.
$_SERVER['REQUEST_URI']
I'm just not sure how to use that within the code that loads the page. I suppose I could write something that will pull out the submitted url info that was just submitted to my database and then plug it in, but is there a way to just use the above code to do it? That seems easier, but when I tried plugging it in, it fails and just gives me a "404 not found" error, so I know I'm not doing it correctly.
It just needs to go in the simple form post action code below:
<form action="comment_box.php" method="post">
I've looked up similar questions, but I couldn't find anything that worked with what I needed, I just got more 404 errors. Any advice would be great, thanks!
If your posting to the same page the user is on you can do
<form action="" method="post">
Leaving the form action blank it will post to what ever the page the form is on.
Not sure what language your using but I assume php you then use
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
// form submit button was pressed your php code here
} else {
// no form submit button was press do nothing maybe show form html code here
}
?>
or what your trying to achieve
$url = $_SERVER['REQUEST_URI'];
Then in PHP
<form action="<?php echo $url; ?>/comment_box.php" method="post">
for example you have a following code
<input type="submit" name="click_me" value="Login">
then in the php, you have to check like
<?php
if(isset($_POST,$_POST['click_me']) && $_POST['click_me']="Login"){
//do your stuff
}
?>

I can't use GET and POST at the same time in PHP

Near the top of my page, I have this:
<?php $id = $_GET['id']; ?>
Then I have some form check conditionals that read from POST:
if (isset($_POST['completeSubmit'])) {
//code
}
And finally, I have an HTML form which looks like this:
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"; ?>" name="complete" method="post">
<input type="submit" id="textButton" name="completeSubmit" value="[mark as complete]">
</form>
The page is initially accessed by using GET with an id variable like this:
http://website.com/page.php?id=1
All subsequent form submissions (which get redirected to the same page) fail. I know you can't send both GET and POST in the same request, but seeing as my form is submitting to $_SERVER['PHP_SELF']."?id=$id" using POST shouldn't it work? This is my first time trying this so it is quite possible I've overlooked something trivial.
You can use get and post at the same time, but you shouldn't. If you want to continue to send the ID this is as simple as:
<form ...
<input type="submit" ...
<input type="hidden" name="id"
value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>" />
</form>
Of course you can not use GET and POST methods simultaneously.
However you can use a query string while sending a form using POST method, which being used to populate $_GET array.
To find a certain error you have to provide more info. At least 2 things:
how does HTML form look
what do yo see in the query string after posting the form.
and errr...
do you use any header redirects in the form processing?

Categories