I have a form that has 2 buttons on it. 1 will send a email using a .php script,and another will submit the form to a .php database. Both of these work great by themselves, but I would like to combine them into 1 button. here is a example of my code:
<FORM name="drop_list" action="<?php echo $editFormAction; ?>"" method="POST" >
<input name="emailForm" type="button" id="emailForm" onClick="sendFormEmail()" value="Email">
<input name="add_patient" type="submit" id="add_patient" onclick=document.drop_list.action='<?php echo $editFormAction; ?>' value="Add Patient">
Here is the java script that is run for the email button:
function sendFormEmail() //email form
{
document.drop_list.action = "html_form_send.php";
document.drop_list.target = "_blank";
document.drop_list.submit(); // Submit the page
return true;
}
I am still very new to php script and javascript, so any help is appreciated.
Just use mail(to,subject,message,headers,parameters) in the send email function.
The function arguments have to be the strings where you store the text of the form fields, so you can fill the mail() function with the email and other data inserted in the user interface.
check this to get to know how to actually call a function declared in the same file Calling a particular PHP function on form submit
Related
I have a website with a landing page, and at the bottom of the page I have a form. All I want to do is show a validation message and redirect the user to the form again so he can look the message without having to scroll.
The code is as follows (it is on the same page):
//validation
if (isset($_POST['submit'])) {
$name = htmlspecialchars(stripslashes(trim($_POST['name'])));
if (!preg_match("/^[A-Za-zÀ-ÖØ-öø-ÿ]/", $name)) {
$name_error = 'invalid name';
}
//i want to redirect as soon as the validation finishes
header('Location: http://localhost/myweb/index.php#contact');
}
<form id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="text" name="name" id="name">
<button type="submit" id="submit" name="submit">Send</button>
</form>
I have tried all of the following:
Use of window.location.href = 'index.php#contact'; in the button (onclick) and in the form (onsubmit)
Use of the header in multiple parts of the code, also with exit();
However, and here is the key, I do not want to use another page of php only for validation (since I would need to use session_start() and I want to avoid cookies) AND and I do not want to use an AJAX XMLHttp Request.
Is it possible to do it?
I'm using #contact in form action like this
<form id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#contact" method="POST">
Did you try this?
I am trying to create a query screen for reports. I have created a php code by getting support from here and other sites. But the problem is; when a user input report serial number and submits it, the page only reload. After reload; when the user enters serial again to the field and hits submit, this time the code works but only for the 1st serial entered, no matter the second serial is.
I have tried to change the parts of my code but could not find a solution.
I am trying to create a system like, user will enter a serial to the field and when hits to submit button; a new window pop out and directs user to a link which has been created based on user input.
For example, user entered "234" as the serial number and hit submit button. The new window will go to the; "example.com/reports/report234.pdf"
Here is the code I have problem with;
<?php
if(isset($_POST['submit']))
{
$seri = $_POST['seri'];
$url = "https://www.example.com/wp-content/uploads/Rapor/".$seri.".pdf";
}
?>
<form method="post" action="<?php echo $url; ?>">
<input type="text" name="seri"><br>
<input type="submit" name="submit" value="Sorgula"><br>
</form>
That's because you're setting the redirect $url as the form action. That results in the following:
Form action is empty, thus the form will be sent to the page itself
The serial number and the $url is created and set as the form action
Now when the user submits the form again it will be directed to the $url set, regardless of what he has filled in the seri field this time
Here is an example of a more correct approach to your problem:
<?php
if(isset($_POST['submit']))
{
$seri = $_POST['seri'];
Header("Location: https://www.ozguncicek.com.tr/wp-content/uploads/Rapor/$seri.pdf");
}
?>
<form method="post">
<input type="text" name="seri"><br>
<input type="submit" name="submit" value="Sorgula"><br>
</form>
Note there's no need to set an action to the form since you're going to redirect the user when he submits the form.
Another important point is checking if seri isn't empty before redirecting the user. That could be accomplished as simple as:
<?php
if(isset($_POST['submit']) && $_POST['seri'])
Redirect after form submit,
<?php
if(isset($_POST['submit']))
{
$seri = $_POST['seri'];
$url = "https://www.example.com/wp-content/uploads/Rapor/".$seri.".pdf";
header("Location: ".$url);
}
?>
<form method="post" action="<?php echo $url; ?>">
<input type="text" name="seri"><br>
<input type="submit" name="submit" value="Sorgula"><br>
</form>
I have a form on on html outside of php...
<form method="post" action="">
<input type="text" name="user"/></br>
<input type="submit" value="submit" name="login"/>
</form>
then call submit button from php and do this
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply"/>
</form>
this;
if(isset($_POST["apply"]))
{ print "it works";}
}
Alright, so the problem is that, "it works" won't print from the second form thats inside the php. it just takes me back to where i came from. Perhaps it's a dumb question, please help though! thanks
The problem is that by the time you're checking if(isset($_POST["apply"])) the login condition becomes invalid because everything is inside the if(isset($_POST["login"])).
Try taking the if(isset($_POST["apply"])) outside the login IF.
Your "apply" code exists only INSIDE the login test code. When you submit that second form, there will be NO login form field, because you didn't include an input/textarea of that name in the second form. So the second form submits, there's no login, and the entire inner code never gets executed. You probably want:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="" name="apply">
<input type="hidden" name="login" value="foo" /> <!-- add this line -->
etc...
I'm not sure to understand what you wanna do with this code but you obviously missed some details :
_You did not set the "action" field in your form tag, so I don't understant how you would like the PHP file to get called ?
_Your code if(isset($_POST['login'])) has no sense, you are testing the existence of a value sent by a validation button, you'd rather whrite isset($_POST['user'])
Hoping to have helped you
Your variables are declared in 2 forms, so there will be 2 calls (completely independant) to your php.
So you could have a second submit button inside your second form:
if(isset($_POST["login"]))
{
print <<<this
<form method="post" action="">
<input type="submit" name="apply" value="Second"/>
</form>
this;
}
if(isset($_POST["apply"]))
{ print "it works";}
I have created a form that has a subit button which will submit the for to a database, and a email button which will run a .cgi script to process the mail. Im having trouble getting the submit button to work.
here is the form buttons
<FORM name="drop_list" method="POST" >
<input name="emailForm" type="button" id="emailForm" onClick="sendFormEmail()" value="Email">
<input name="add_patient" type="button" id="add_patient" onClick="addPatient()" value="Add Patient">
</FORM>
And here is my javascript
function sendFormEmail() //email form
{
alert ("Email, this is disabled atm");
document.drop_list.action = "html_form_send.php"
document.drop_list.submit(); // Submit the page
return true;
}
function addPatient() //Post form to data base
{
alert ("Post to database");
document.drop_list.action = <?php echo $editFormAction; ?>;
document.drop_list.submit(); // Submit the page
return true;
}
The sendFormEmail() works fine, but when I try to use addPatient(). The form will not submit and something in the document.drop_list.action = ; line completely breaks the java script.
Thanks in advance for your help.
-Gregg
You haven't shown the value of $editFormAction, but I'll bet it doesn't have quotes around it. So you need to write:
document.drop_list.action = "<?php echo $editFormAction; ?>";
If you checked in the Javascript console, you should have seen an error message about an undefined variable or undefined not having a php method. And then if you looked at the JS source, you would have noticed that the script name doesn't have quotes around it, like it does in sendFormEmail.
I was just trying to submit a simple form to the same page but when it is submitted it will call PHP function on the same page. However I was trying to do some JavaScript validation before submission. So I want to know what the difference between using onSubmit call js function in the form tag and onClick call js function with button.... This is what I am currently trying to do.
<?php
function tobecalled()
{
echo "This was run";
}
?>
<html>
<head><title>Testing</title>
<script type="text/javascript">
function testResults (form)
{
var TestVar = form.inputboxname.value;
if(TestVar == '')
return false;
else
return true;
}
</script>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" onSubmit="return testResults(this);">
<input type="text" name="inputboxname" />
<input type="submit" value="Save" name="submit" />
<?php
if(isset($_POST['submit']))
tobecalled();
?>
</form>
</body
</html>
It works..
But if I make (Submit Via JS)
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
...
<input type="submit" value="Save" name="submit" onClick="return testResults(this);"/>
...
Its still calls the PHP function tobecalled()--Why? I am expecting it not call. How do it work?
The reason that it is allowing it to go through is because you are passing this in the onclick event. In this instance this is referring to the submit button not the form as required by the function.
Thus form.inputboxname.value returns undefined which is not '' (empty string) and therefore the testResults function returns true. So the submit is then activated.
The difference is this. this points to a different object in onClick than in onSubmit. Your function expects a form to be passed, but when you use onClick, you give it the submit button. That's why the second method doesn't work as expected.
Because regardless of whether you add your javascript to the onsubmit of the form or the onclick of the submit button the form will still be submitted by the submit button. That means that a request is being sent back to the server and $_POST['submit'] will be set. Since that variable is set you find your function being called.