I am sorry if has any unclear sentences beacuse I am bad in explaining thingy..
So i have three sample file name.html, time.html and insert.php.. in name.html, after i click the button and it will pop up another window (time.html). Then in time.html after i click the submit button, the insert.php will execute.
name.html
<form method = "post">
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
time.html
<form method="post" action="insert.php">
<input type="time" name="time_name" id="my_time">
<br><br>
<input type="submit" name="myButton" id="myButton">
</form>
insert.php
<?php
$name = $_POST['student_name'];
$time = $_POST['time_name'];
if(mysqli_query($connect, "INSERT INTO student (student_name, time)
VALUES ('$name', '$time')")){
....}else{ ...}
My question: Is there a way that the insert.php can retrieve both name and time information in the html files but not only in time.html.
If I add in the insert.php file in name.html, the php file will execute first.
<form method = "post" action= "insert.php">
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
::Most of the sentence makes no sense, if any unclear sentence please let me know.. Also, i couldn't think any better title for this question..
Edit: Seems like i found the solution.. I just add an action in the name.php.
In name.html i change to name.php
<form method = "post" action = "<? include 'time.php' ?>" >
<h3>Name: </h3><input type ="text" name= "student_name" id="studentName">
<input type="button" name="submit">
</form>
In time.html change to time.php
Thanks for helping me out!!
Your question: Is there a way that the insert.php can retrieve both name
and time information in the html files but not only in time.html.
If I add in the insert.php file in name.html, the php file will
execute first.
The way you explained is a bit confusing ! but anyway if you want include the php to your name.html !
1. Either instruct Apache to treat .html files as PHP(See This) or make it as a .php file
and if you want to restrict your php code from executing first or automatically ! update your code with
if(isset($_POST['something'])){ //all other stuff here }
On your code
<?php
if(isset($_POST['submit'])) { //executes only when the submit button clicked !
$name = $_POST['student_name'];
$time = $_POST['time_name'];
if(mysqli_query($connect, "INSERT INTO student (student_name, time)
VALUES ('$name', '$time')")){
....}else{ ...}
You can also make some more conditions to check whether $name & $time values are entered or not also simply you can validate it with making it as required in html itself
Also in a better way you can do the same with AJAX and some JS See the answer here
You have two files with two forms. And you have 1 php file to process both. And you want the data from both files in your insert.
Then why don't you create only 1 file, 1 form, ask for both info, insert once.
So your form would become:
<form action="index.php" method="POST">
<h3>Name: </h3><input type="text" name="student_name" id="studentName">
<h3>Time: </h3><input type="time" name="time_name" id="my_time">
<input type="button" name="submit">
</form>
Obviously this above code is to illustrate, I do not take care of everything. Then you have to check SQL injection, validate your inputs, ...
As far as I know, there is no way to get data from 2 forms into a single action, since every time you click submit, it executes the action.
Unless your first form defines it's action as the second form. Then the second form creates a to pass the result of the first form along with it's input to the action file that does the insert to database.
Related
I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.
I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.
single.php:
echo
'<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;
<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>
emailForwarding.php:
if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
if(isset($_POST['comment'])):
$EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.
The form is submitted the same number of times as the number of the forms on the page.
Inserting include() before tag of single.php disabled duplicate submission.
Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.
Is the echo statement user any loop. Can you try by giving a different name to FORM?
<form method="post" action="" name="form-$i" class="commentForm">
I have an input type text box as follows
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
Delete Account
I need to pass the value entered in the input type text to deleteaccount.php
I can do with help of jquery, no problem, i need a pure php solution...
I tried using sessions, but problem is how to read the value in input type when link is clicked.. $_POST is also not working...
i cannot use form because this is a form in another form so html5 is not allowing nested forms, sorry should have mentioned that earlier
the following is not working on deleteaccount.php
if (isset($_POST['deleteprofilebutton']))
{
$delete_profile = strtolower($_POST['deleteprofileconfirmation']);
}
Make your link as
href="../controllers/deleteaccount.php?id=$ID_VALUE"
and update the POST to GET
if (isset($_GET['id']))
{
$delete_profile = strtolower($_GET['id']);
}
That would be GET now.
Make sure users with no privileges can hit this url and delete the profiles. Do check the user rights before processing the delete operation.
You can do this using form
<form action="../controllers/deleteaccount.php" method="post">
<input type="text" name="deleteprofileconfirmation" id="deleteprofileconfirmation" class="editprofileinput">
<input type="submit" class="deleteprofilebutton" name="deleteprofilebutton" id="deleteprofilebutton" value="Delete Account">
</form>
You could also give each one a unique name and just check the $_POST for the existence of that input.
<input type="submit" name="deleteprofileconfirmation" value="Delete" />
<input type="submit" name="submit" value="Submit" />
And in the code:
if (isset($_POST['deleteprofileconfirmation'])) {
//delete action
} else if (isset($_POST['submit'])) {
//submit action
} else {
//no button pressed
}
I have a form with few input fields and a update option ,suppose if i have 10 fields and i update only first two fields and not the rest who can i display a message that only the first to field A and B are updated using PHP and Mysqli
in the screenshot if i update the value of bill id and origin i should display message out in some other page that says that bill id and origin has been updated with "xyz " value
One way is to fetch fields with real and old name i.e.
Select name,name as old_name from table
In html
<input type="text" name="name" value="name from db">
<input type="hidden" name="old_name" value="old_name from db">
After submit form you can check it by.
And check after post.
If(post['name']==post['old_name'])
Something like this.
Another way is from js by checking onchange event
well it'll be something like this ..
html:
<html>
<body>
//action is defining the php file's name which the form will be sent to ;
//method is defining the method which the form will be sent with
<form action="updateCheck.php" method="post">
<input type="text" name="input-filed-1"/>
<input type="text" name="input-filed-2"/>
<input type="text" name="input-filed-3"/>
<input type="text" name="input-filed-4"/>
<input type"text" name="input-filed-5"/>
<input type="submit" value="submit/>
</form>
</body>
</html>
create a file named updateCheck.php in the same directory and enter the following code
<?php $updateMessage = "you have updated the following fields";
for($x = 0 ; $x<=5 ; $x++)
{
// trim for erasing the whitespaces
// that if clause checks if there was data entered in the update input fields
if(isset($_POST["input-field-".$x]) && trim($_POST["input-field-".$x]))
{
$updateMessage.= "input-field-".$x ;
}
}
echo "<script type='text/javascript'>alert('".$updateMessage."')</script>"
?>
I haven't tested the code .. but study it carefully and you'll see how it's done
So you can include for example functions.php in your file and then you can use different functions in that page to validate user input.
But how can I process multiple forms using the same file?
Example:
include formprocessor.php
formprocessor would contain all the functions related to each form:
Form A action=formAfunction()
Form B action=formBfunction()
Form C action=formCfunction()
Can formprocessor.php contain a function specifically for handling each form?
Each form would have their own function to process their data.
If this is possible it is way more convenient than having a processor file for each form on a site. 10 forms 10 processing files?
Or 10 forms 1 processing file.
You can see an input type=hidden that contains a name for each form, then in your form processor, read the name that was submitted and go to the specific part of the code accordingly.
You could add a name to the submit button for each form in html
<form method='post' ... >
....
<input type='submit' name='formA' ...>
</form>
<form method='post' ... >
....
<input type='submit' name='formB' ...>
</form>
Then use something like this in your php code to handle it:
if(isset($_POST['formA'])){
formAfunction(); //handle formA
}elseif(isset($_POST['formB'])){
formBfunction(); //handle formB
} //.... etc
Have a hidden process inside your form. IE
<form id = "login" action = "calls.php" method = "POST">
<input id="process" name="process" type="hidden" value="loginpr">
<input type="submit">
</form>
<form id = "logout" action = "calls.php" method= "POST">
<input id="process" name="process" type="hidden" value="logoutpr">
<input type="submit>
</form>
Then you have your calls.php file which switches based off the the process called.
if(isset($_POST['process'])){
switch($_POST['process']) {
case 'loginpr':
loginfunction();
break;
case 'logoutpr':
logoutfunction();
break;
}
}
else { echo "The process is not linked within this form"; }
calls.php would be your formproccessor.php file
When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?