Double form will not submit second form - javascript

Hi I have two HTML forms when the one is submit a JavaScript function submits the second the one form works but the second doesn't I'm not sure if it is the forms or the post pages can any one help.
The one form sends an email this is sending the email correctly sending the correct data to the correct email address.
The second form is meant to upload a file it doesn't seem to be doing anything at all there are now errors displayed to the screen I have done a try catch and nothing is displayed i have also looked into the logs and nothing is displayed I'm
HTML
<div id="loginborder">
<form id ="upload" enctype="multipart/form-data" action="upload_logo.php" method="POST">
<input name="userfile" type="file" />
<input type="submit" onsubmit="alert()" value="dont press" disabled>
</form>
<div id="login">
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="subject" value="Can you create me a Contributors account">
<input type="text" name="first_name" id="first_name" placeholder="Name">
<input type="text" name="company" id="company" placeholder="Company Name">
<input type="checkbox" id="tc" onclick= "checkbox()">
<input type="submit" id="submit" onsubmit="alert()" name="submit" value="Register" disabled>
</form>
</div>
</div>
<?php
}
else
// the user has submitted the form
{
// Check if the "subject" input field is filled out
if (isset($_POST["subject"]))
{
sleep(5);
$subject = $_POST["subject"];
$first = $_POST["first_name"];
$company = $_POST["company"];
$therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";
}
echo "$therest <br>";
$first = wordwrap($first, 70);
mail("careersintheclassroom01#gmail.com",$subject,$name,$therest,"subject: $subject\n");
echo "Thank you for sending us feedback";
header( "refresh:5;url=index.php" );
}
?>
</body>
</html>
javascript
<script type="text/javascript">
function alert()
{
document.getElementById("upload").submit();
}
function checkbox(){
if (document.getElementById("tc").checked == true)
document.getElementById("submit").disabled = false;
else
document.getElementById("submit").disabled = true;
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
</script>
Upload_Logo.php
<html>
<head>
</head>
</html>
<?php
$uploaddir = "./images/";
echo $uploaddir;
mkdir($uploaddir, true);
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<br />";
echo " <b>Your media has been uploaded</b><br /><br /> ";
?>
the <?php echo $_SERVER["PHP_SELF"];?> on the second form calls the php at the bottom of the page this is the one that is working it is the upload_logo.php that is not currently working any help would be much appreciated

You're trying to submit 2 forms at once. That can't work, as your browser can only be directed to 1 page at a time, so your attempt to submit the upload form with JavaScript is cancelled by the contact form being submitted. I'd suggest that you move the file input into the same form as the contact fields, and handle them both in your "the user has submitted the form" section.
Something like this should do the trick:
<?php
if (!isset($_POST["submit"]))
{
?>
<div id="loginborder">
<div id="login">
<form enctype="multipart/form-data" method="POST">
<input name="userfile" type="file" />
<input type="hidden" name="subject" value="Can you create me a Contributors account">
<input type="text" name="first_name" id="first_name" placeholder="Name">
<input type="text" name="company" id="company" placeholder="Company Name">
<input type="checkbox" id="tc" onclick="checkbox()">
<input type="submit" id="submit" name="submit" value="Register" disabled>
</form>
</div>
</div>
<?php
}
else
// the user has submitted the form
{
// Check if the "subject" input field is filled out
if (!empty($_POST["subject"]))
{
sleep(5);
$subject = $_POST["subject"];
$first = $_POST["first_name"];
$company = $_POST["company"];
$therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";
echo "$therest <br>";
$first = wordwrap($first, 70);
mail("careersintheclassroom01#gmail.com",$subject,$name,$therest,"subject: $subject\n");
echo "Thank you for sending us feedback";
header( "refresh:5;url=index.php" );
}
if (isset($_FILES['userfile']['name'])) {
$uploaddir = "./images/";
if (!file_exists($uploaddir)) {
mkdir($uploaddir, true);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
echo "<br />";
echo " <b>Your media has been uploaded</b><br /><br /> ";
}
}
?>
</body>

try this after $uploadfile = ...
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

when you submit 2nd form e-mail would be generated becausue it triggers if(isset($_POST["subject"])) condition and the code will follow the next commands.
But when you submit 1st form, it will call onsubmit="alert(); function and that function again submits the same form because of these.
function alert()
{
document.getElementById("upload").submit();
}
so you are just triggering a never ending loop.
My solution is
<script type="text/javascript">
function alert()
{
function checkbox(){
if (document.getElementById("tc").checked == true)
document.getElementById("submit").disabled = false;
else
document.getElementById("submit").disabled = true;
}
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
</script>
I'am not 100% sure about your requirement. hope you can get the point what i'am making. gl!

Related

How could I replace a form with already inserted text after unsuccesfull finding in database?

I have the code in Participate.php file which has a form:
<form method="post" action="input.php" id="Form">
<input type="text" class="form-control" name="txtName" maxlength="20" required style="margin-bottom:20px">
<input type="email" class="form-control" name="txtEmail" aria-describedby="emailHelp" required style="margin-bottom:20px">
<input type="submit" class="btn btn-success" id="btnsubmit" value="Zgłoś się" />
</form>
After unsucessful submit (I check if inserted mail already exist in database) and if no exist I want to refill value for form. This is the input.php code:
<?php
$name = $_POST['txtName'];
$mail = $_POST['txtEmail'];
$description = $_POST['txtDescription'];
$connect = new PDO("mysql:host=4*****3;dbname=3*****b", "3***b", "****");
$connect->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$q=$connect->prepare("SELECT mail FROM konkurs WHERE mail LIKE (?)");
$q->bindValue(1, $mail);
$q->execute();
$row_cnt = $q->rowCount();
if($row_cnt == 0){
$query = $connect->prepare("insert into konkurs(name,mail,description)
values(?, ?, ?)");
$query->bindValue(1, $name);
$query->bindValue(2, $mail);
$query->bindValue(3, $description);
try {
$query->execute();
echo ("<script LANGUAGE='JavaScript'>
window.alert('Sent.');
window.location.href='index.html';
</script>");
exit;
} catch (PDOException $e) {
die($e->getMessage());
}
} else {
echo ("<script LANGUAGE='JavaScript'>
window.alert('This mail already exist.');
window.location.href='Participate.php';
document.getElementById('txtName').value = 'nothing';
</script>");
}
?>
The thing is it's redirecting to Participate.php after unsuccesfull sumbition. But it doesn't refill the form.
First of all, you must change your HTML to include the id attribute, for example:
<form method="post" action="input.php" id="Form">
<input type="text" class="form-control" id="txtName" name="txtName" maxlength="20" required style="margin-bottom:20px">
<input type="email" class="form-control" id="txtEmail" name="txtEmail" aria-describedby="emailHelp" required style="margin-bottom:20px">
<input type="submit" class="btn btn-success" id="btnsubmit" value="Zgłoś się" />
</form>
Then, you must move your logic to the same file as the form container (here Participate.php), and remove the redirection for failures. Otherwise, you'll get no visible results, as the redirection would prevent further JavaScript code from running.
// Updated to prevent syntax errors with multiline strings
echo "<script>"
. "window.alert('This mail already exist.');"
. "document.getElementById('txtName').value = 'nothing';"
. "</script>";

Entered Fields should not be cleared with alert box is displayed in php

I am trying to display a message if the amount entered greater than 5000 alert box or echo messsage "Enter a pan card no" as to be displayed when I click on submit button. In my code Message gets displayed but all the fields which data has been already entered gets cleared. I dont want the data which is entered to get cleared.
Here is the code.
<?php
include_once "db.php";
if(isset($_POST['submit'])) {
$amount = mysql_real_escape_string($_POST["amount"]);
$firstname=mysql_real_escape_string($_POST["firstname"]);
$phone=mysql_real_escape_string($_POST["phone"]);
$address=mysql_real_escape_string(stripslashes($_POST["address"]));
$pan=mysql_real_escape_string(stripslashes($_POST["pan"]));
$query= mysql_query("INSERT INTO payment (amount,selector1,firstname,lastname,email,phone,address,country,state,pan)VALUES('$amount','$selector1','$firstname','$lastname','$email','$phone','$address','$country','$state','$pan') ") or die(mysql_error());
if($amount>="5000")
{
echo "Enter Pan Card No";
}
else {
$id=mysql_insert_id();
$_SESSION['sess_user'] = $id;
$_SESSION['lastname'] = $lastname;
$_SESSION['address']=$address;
header("Location:successreg.php");
}
}
?>
<form action="" method="post" name="payment">
<div class="w3l-user">
<input type="text" name="amount" placeholder="₹ My Contribution" required="">
</div>
<input type="text" name="firstname" placeholder="Firstname" required="">
<input type="text" name="pan" placeholder="Pan Card No" >
</div>
<div class="btn">
<input type="submit" name="submit" value="REGISTER"/>
</div>
</div>
<div class="clear"></div>
</form>
</div>
</div>
In order to show values, after for submission you should get values from $_POST and use them in input values:
$firstname = '';
if(isset($_POST['submit'])) {
// write below line after `if`
$firstname = $_POST['firstname'];
Now, change your HTML to:
<input type="text" name="firstname" placeholder="Firstname" required="" value="<?php echo $firstname; ?>">
For Radio buttons:
$firstRadio = $secondRadio = '';
if(isset($_POST['submit'])) {
// if first radio is selected which you will know from $_POST['firstRadio']
$firstRadio = 'selected';
And in HTML:
<input type='radio' <?php echo $firstRadio;?> value=1 name='firstRadio' />

submitting content to a .txt using php and staying on the same page and not go to the php page

I wanna submit/write content to a .txt file and i have to used php to do it, but i don't wanna open the php page and wanna stay on the same page.
How can I do this?
Index.html
<form action="writer.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
writer.php
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "|| \n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo " written to file";
}
}
else {
die('no post data to process');
}
?>`
You could simply use iframes, easier alternative to AJAX.
<iframe name="panel" style="display:none;"></iframe>
<form action="writer.php" method="POST" target="panel">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
...and as everyone here is yelling, consider learning AJAX.
create a php file with following in it.
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
file_put_contents('mydata.txt', $input, FILE_APPEND | LOCK_EX);
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php if(isset($message)) echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>

Show hide part of php page

I have a php page. In the bottom there is a contact form. What I want to do is to hide the contact form when the mail is sent and instead show a thank you part. Means that when customer comes first time to the page the form show up. After submit the thank you part show up.
I have seen it done but have no clue how.
I have an idea that when the page loads it must check a variable to see if the mail was sent, but perhaps this is wrong.
When I need stuff like this, I usually name the submit button "submit" and checks if it is set...
<input type="submit" name="submit" value="Send form">
And the php (Use $_GET or $_POST accordingly)...
<?
if(isset($_POST['submit']))
{
// Show "Thank you"
}
else
{
// Show form
}
?>
When I do things like this I tend to add an button to my form and give it a name
<button type="submit" id="submit" name="submit">Submit</button>
Then in my PHP check if the form has been submitted
<?php
if(isset($_POST['submit'])) {
// Actions After Submit
}
else
{
// Load The Form
}
?>
It's rather quite simple; add exit("Message..."); after mail()
In your PHP where the mail(...) is located, you would do the following:
mail($to,$subject,$message,$headers);
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
Here is a complete basic solution using pure PHP, since no code has been provided.
The following is meant to be run as everything inside the same file.
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = 'email#example.com';
$mail_from = $_POST['email'];
$subject = 'Message sent from website';
$message = $_POST['message'];
$name = $_POST['name'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
} else {
echo 'Sorry, your message could not be sent. The error has been logged.';
}
} // closing brace for if(isset($_POST['send']))
?>
<form method="post" action="">
<p>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</p>
<p>
<label for="email">Email</label>
<input type="text" id="email" name="email" />
</p>
<p>
<label for="message">Message</label>
<textarea id="message" name="message" rows="6" cols="30"></textarea>
</p>
<p>
<input type="submit" name="send" value="Send message" />
</p>
</form>
It's very simple, just if and else,sample form is below
<?php
if(isset($_POST['submit'])) { ?>
//write your other actions on form data
<h2>THANK YOU</h2>
<?php
} else { ?>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label><h4>Username:</label>
<input type="text" name="username">
<input type="submit" name="submit" value="submit"/>
</form>
<?php }
?>

Need help adding Email Validation and hiding form after submit

I am new to PHP and am having trouble with adding email validation and then hiding the form after pressing the submit button. I have an error.php, thankyou.php and formmail.php. The PHP code below is that of formmail.php. I just don't know what to write for email validation for this specific code. I've tried copying some PHP codes from other sites but it just doesn't match up with my code. And as far as hiding the form after submit, I really don't know what to do with that.
Here is my HTML:
<div id="contact">
<div id="contact-left">
<form id="ajaxsubmit" action="formmail.php" method="post">
<div class="form">
<div class="formblock">
<h2>Name</h2>
<input name="name" type="text" class="required txt" id="name" />
</div>
<div class="formblock">
<h2>Email</h2>
<input name="email" class="required txt" type="text" id="email" />
</div>
<div class="formblock">
<h2>Message</h2>
<textarea name="comments" cols="" rows="" id="comments"></textarea>
</div>
<input name="Submit" type="submit" class="subbtn" value="Submit" />
<div id="message"></div>
</form>
</div>
</div>
Here is my PHP:
<?php
// Insert your email/web addresses and correct paths
$mailto = 'myemail#email.com' ;
$from = "http://website.com" ;
$formurl = "http://website.com/formmail.php" ;
$errorurl = "http://website.com/error.php" ;
$thankyouurl = "http://website.com/thankyou.php" ;
// Place Your Form info here...
$firstname = ($_POST['name']);
$email_from = ($_POST['email']);
$comments = ($_POST['comments']);
// Check If Empty
if (empty($firstname)) {
header( "Location: $errorurl" );
exit ;
}
// Add more Validation/Cleaning here...
// Place your Corresponding info here...
$message =
"Name: $firstname\n\n" .
"Email: $email_from\n\n" .
"Comment: $comments\n\n"
;
// Leave Alone
mail($mailto, $from, $message,
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep );
header( "Location: $thankyouurl" );
exit ;
?>
Please let me know if you can assist me or need any other information from my html.
Thanks in advance!
To validate email in php, follow this link,
if (filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_from) email address is considered valid.";
}
To hide the form, using jquery,
$(document).on('submit', '#ajaxsubmit',function(){
$(this).hide();
});
or using pure javascript,
Change HTML,
<form id="ajaxsubmit" action="formmail.php" method="post" onsubmit="myFunction()">
and JS
function myFunction(){
var form = document.getElementById("ajaxsubmit");
form.style.display = 'none';
}

Categories