Hello i have a strange problem i have the following form
echo "<td class='contact-delete'>
<form method='post' action='update_record.php'>";
echo "<button type='submit' value='" . $row['ID_PER'] . "' name='recordID' id = 'recordID'>edit</button>";
echo "</form>";
in the update_record.php I have the following code
$id2update = $_POST['recordID'];
echo $id2update ;
session_start();
if(!$_SESSION['logged']){
header("refresh:3; url=start_page.php" );}
// above this part of code there is an other form with a submit button
else if(isset($_POST['submit'])){
echo $id2update ;
}
The problem is that the first echo outputs the variable as it supposed but the second echo does not output anything it is null. Can anyone give me an explanation?
edit:
The 2nd echo is being called but the value is null!
You say "above this part of code there is an other form with a submit button". I assume it is that you are trying to echo with the second submit? In that case the data is never submitted. When you submit an HTML form only that form is submitted. Any inputs/buttons/etc in other forms on the page are not sent to the server and so not available in the PHP code. This is by design.
Thus your code as it stands should never reach the second echo.
(Technically only non-disabled form elements with non-null name in the current form are submitted)
Edit:
I think I now understand the comment in your code. You have one form in another file which submits to update_record.php In that request you render a new form (as part of update_record.php) with a submit button. That new form submits to itself with the submit button. If this is correct then the point is that the submit of the form from update_record.php (the one with the submit button) is a new request. You set the value of $id2update in one request but then do a new request and in that it is not set. You should include that value as a hidden input in the form rendered in update_record.php:
<input type="hidden" name="recordID" value="<?php echo $id2update;?>" />
then, when the second request is made (when the update_record.php form is submitted) the vlaue will be fetched again.
Each request must be treated for itself - the server does not automatically know which requests go together (up to data saved in the session of course).
Related
I'm building a registration form in Concrete5 but beforehand I need to retreive the User Attributes.
I'm using hidden input to get the values as so:
<input type="hidden" name="akID[<?php echo UserAttributeKey::getByHandle('school')->getAttributeKeyID(); ?>][value]" value='<?php echo $_POST['full_name']; ?>'/>
However I can only get the value if I make a mistake beforehand, i.e. incorrect confirm password and then resubmit the form.
Any ideas on how to get the values without doing this?
Kind Regards
Your example is a bit confusing.
First let me explain why it doesn't work the way you want it to.
You are giving your hidden field a value from $_POST. $_POST only exists once you actually post the form. So on first load of the page, $_POST doesn't exist so $_POST['full_name'] is null.
When you submit the form and there is an error however, the same page reloads but this time $_POST exists since the page reloads after submitting the form.
Here's what is confusing. If on reload $_POST['full_name'] has a value it means you already have a 'full_name' field probably as a text input box. Why then do you want to have a hidden field with the exact same value?
If what you want in this hidden field is the value of a user attribute you need to do 2 things:
1- make sure the user is logged in, else no attributes are available
2- get the user info object to get the attribute value from like so:
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
2- modify the value of the hidden field like so:
value="<?php echo $ui->getAttribute('attribute_handle'); ?>"
i´ve got a little problem with my php code.
The code contains a form that reloads the document. But after the reaload I cant read the POST data. Here is the HTML code:
<form action="config_page.php" method="post">
... some Inputs
<input type="submit" value="Save" name="config_btn" class="submitbtn_2">
</form>
On top of config_page.php ive got this PHP code:
if(isset($_POST["config_btn"])){
echo "isset";
//Some Database writing
}else{
echo "is not set";
}
Bizarrely, the output "is not set" always appears after submiting the form, but Database Changes are applied anyway... (Database Changes are only performed if the isset statement is true)
Can someone figure out the problem?
Thanks for your help!
POST values will always set after post or submit form with form post method
echo "is not set"; always true until you not click on submit or post any values. after submit click you will find $_POST["config_btn"] is set true so db queries runs.
so keep your form in else part.
So :-
if(isset($_POST["config_btn"])){
echo "isset";
//Some Database writing
}else{
echo "is not set";
}
When page loaded it's goes to else condition and will echo "is not set";
When you click on submit POST values found and it's runs your query.
if you redirect page again then again post values will be end and goes to else.
I am making a page that has a bunch of fields that allows the user to enter in information and submit it to a database. This page is called 'add.php' I created a 'form' tag and had the information posted to another page called 'process.php' where the information is collected, then inserted into the database. I want the user to know whether it was successful or not, so I was wondering how to tell the user something specific on the 'add.php' page. like "insertion successful!" at the top of the page.
I thought of including the 'process.php' code in 'add.php', then calling the 'add.php' in the action of the form, but the code gets called the first time the page is loaded, which inserts a completely blank entry into the database.
Should I implement some sort of flag that is only set to true after the 'submit' button is clicked? Or is there another way to update the page and tell the user the status of the insertion?
I can paste the relevant code as needed. :)
Thanks!
Assuming that you are using the post method in your form and php, you can simply check if a post was made:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// form was posted, process and display output
}
else
{
// nothing was posted, normal get request, show form
}
just check if query worked well. If no exception was thrown, it mostly has, and the add appropriate message with output.
First you need to check and handle errors
try
{
}
catch(Exception $e){
header('Location:oldlocation.php?succ=0')
exit();
}
header('Location:oldlocation.php?succ=0')
exit();
If all goes well, you can also redirect to a new location(as shown in code). This has to be done properly, you may redirect back to the old location, with additional data like
oldlocation.php?succ=1;
If anything goes wrong redirect to
oldlocation.php?succ=0
Then fetch the succ using $_GET["succ"] and print appropriate message.
If you din get, comment.
Here's what I would do...
Keep your processing data in one file, and include the form file at the end
//add.php
//if the form is submitted make the database entry
if(isset($_POST['foo']) AND $_POST['foo'] != '')
{
//code to process form submission
$success = 'success!';
}
//include the form
include addform.php
in addform.php put your form. Include an 'isset' that is watching for $success to alert that the entry was successful
//addform.php
<?php if(isset($success)){ echo "<h2> Data successfully entered! </h2>";} ?>
<form action='' method='POST'>
<input type='text' name='foo' />
//etc
</form>
So once you submit the form, the code starts at the top of add.php - the 'isset' sees the $_POST submission, runs the form submission code and sets the success variable. Then, it includes the form page. The form page has an 'isset' that is watching for the success variable. When you first navigate to the page, or if you refresh, the add.php code will skip the first code block (the form submission stuff) and won't make a database submission or set the success variable.
I have an html form that is filled with the values I get from a MySQL Database query. The query is by an id that is sent via GET.
I send the id to the form along with a button that is surrounded by an anchor <a>:
<a href='editRQS.php?id=$row[0]'><button class='edit'>Edit</button></a>
$row[0] is filled by the proper id and I'm sure it's working ok. When I click the button the url is sent carrying the id. It is then received at the other page like this:
<?php
$id = -1;
if(isset($_GET['id'])){
$id = $_GET['id'];
echo "<label class='exists' id='idRequest'> $id</label>";
}
if($id != -1 ){
echo '<form id="findRequest" class="hide" >';
} else {
echo '<form id="findRequest" class="show" >';
}
?>
The problem is that the id arrives at the page - but then it disappears. Any help will be greatly appreciated.
EDIT
The page seems to reload, i have no idea why, i have some javascript that manages some of the page functionality and the only thing that could cause this is an event handler for when the another form is submitted but I have prevented it like this:
$('anotherForm').on('submit',function(e){
.
.
.
e.preventDefault();
}
EDIT
Thanks to everybody that helped, i have yet to find the reason it doesn't work, i tried dissabling javascript and the page stoped reloading so i guess it's another thing that's messing my page up
The page is reloading because the form gets submitted. It isn't enough to preventDefault the form submission. Try e.preventDefault(); e.stopPropagation(); the latter preventing form submission when a submit button is clicked (clicks are still handled, and, in some cases propagate to the form, that is then submitted.
Also, I see you're using jQuery. Just let your event handler return false. jQuery automatically converts a false-returning handler to a e.preventDefault() ||e.returnValue = false e.stopPropagation() || e.cancelBubble = true; construction. Or you could write the code yourself...?
Check your register_globals setting, if it is on you will be overriding $_GET['id'] with the first statement.
You can easily check it by renaming $id to something other then $id.
Is there a way to check the data sent by a form to a PHP page return to the form page WITHOUT resetting the data sent and show a error?
The form has 20 fields and I need to check one of them on a bd. If it fails the user may be redirected to the form page with the form populated and displaying a error message on the field which is 'wrong'.
I would like any advice of a technique instead of populating each field using PHP.
UPDATE:
I do not want to use any solution that involves repopulate the fields by myself!!!
I want a solution that return to the form page populated with the previous values. I've tried something like js.history.back or window.back(). But the form returns empty...
UPDATE: If you are looking for this type of behavior, nowadays there are several different techniques to achive this. I'm currently using jQuery (Ajax).
In your form fields HTML, add posted values as field value.
e.g:
<input type='text' name='email' value='<?php echo $_POST['email']; ?>' />
Yeah, just check the forms beforehand, make sure you run the form HTML after the validation procedure, and if validation fails, reload the form with all of the prior information in the fields.
It might look something like this:
<?php if(isset($_POST['submit']){
// validation code goes here with a trigger variable to
// contain true or false depending on the outcome
}
if($trigger==false){
// load up your post data here
}
echo '
<!-- form HTML goes here -->
';
On your Form Validation page
<?php
$form_values = $_POST;
//Your Form Validation
//$form_validated = TRUE/FALSE; // FALSE if there were errors
if ($form_validated) {$form_values = array();}
$_SESSION['form_values'] = $form_values; /* if your form validation page and form page is not on the same page and can also use the same technique for error messages */
?>
On your Form page for each input field.
<?php
$form_values = $_SESSION['form_values']; //if your form validation page and form page is not on the same page
unset($_SESSION['form_values']);
echo '<input type="text" name="user_name" value="'.$form_values['user_name'].'" />';
?>
Different input types will need to handle the $form_values array differently but this should get your started.
If you are not using a seperate form validation page, you can get the form values directly form the $_POST array.