Basically, I'm trying to do is to reset all the form values after submission. I've tried every post but not got success. Please tell me the way of resetting values after submission so that after refreshing the page the form doesn't submit again. Thank you Here is my piece of code. I've tried headers but it is giving me errors so tell me another way of resetting the submit post after submission.
<form name="myForm" id="myForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" role="form">
<div class="form-group">
<label>Name</label>
<span class="Name_input"> </span>
<input name="Name" id="Name" type="text" class="form-control" required placeholder="Your name" />
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Email</label>
<span class="Email_input"> </span>
<input name="Email" id="Email" type="text" class="form-control" required placeholder="Email address" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Phone</label>
<input name="Phone" id="Phone" type="text" class="form-control" placeholder="Phone number" />
</div>
</div>
</div>
<div class="form-group">
<label>Subject</label>
<span class="Subject_input"> </span>
<input name="Subject" id="Subject" type="text" class="form-control" required placeholder="Subject" />
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="Write you message here..." style="height:100px;"></textarea>
</div>
<input type="Submit" name="Submit" value="Send message" class="btn btn-two"/><p><br /></p>
</form>
if(isset($_POST['Submit']))
{
$User_Name=$_POST['Name'];
$User_Email=$_POST['Email'];
if(isset($_POST['Phone']))
$User_PhoneNumber=$_POST['Phone'];
else
$User_PhoneNumber="";
$Feedback_Subject=$_POST['Subject'];
$Feedback_Message=$_POST['message'];
$Inserted=mysqli_query($con, "Insert into feedback (Name , Email, Phone_Number, Subject, Message )
VALUES('" . $User_Name . "', '" .$User_Email . "', '" . $User_PhoneNumber. "', '" . $Feedback_Subject . "','" . $Feedback_Message . "')");
}
Your form should not resubmit just because you refresh the page. However, I just using header(), to redirect to a fresh page, even if the same.
So, straight after the sql query:
header("Location: page.php");
and replace page with which ever page it is.
also, put
<?php ob_start(); ?>
at the very top (before html) of your script to prevent error: Cannot modify header information - headers already sent
...and for your other question:
$(window).bind("pageshow", function() {
$("#formvalue1,#formvalue2,#formvalue3").val('');
});
this will get triggered when the page is shown, even if not reloaded.
Related
this is a simple html form and am trying to link it with javascript code so that the user can be able to send a message to contact my client.
<div class="container">
<div class="form">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage"></div>
<form action="" method="post" role="form" class="contactForm">
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group col-md-6">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
</div>
I don't know if I should use php for that or javascript, or if there is any library that can help me to simplify my work
You can use both PHP or JavaScript to sending data to the server.
If you use PHP, then you should specify form action, if you leave it empty, your data will be sent to the current page. Also, you can send data by javascript or other related libraries like jQuery.
In this case you can use XMLHttpRequest or ajax
I have a comment section in my page.
<form class="reply-form" id="reply-form" method="POST">
<input type="hidden" name="id" value="<?php echo $post->ID;?>" id="postid">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="sr-only control-label" for="name"></label>
<input id="name" name="name" class="form-control" placeholder="NAME" required="" type="text">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="sr-only control-label" for="email"></label>
<input id="email" name="email" class="form-control" placeholder="E-MAIL" required="" type="text" >
</div>
</div>
<div class="form-group">
<div class="col-md-12 mb30">
<label class="sr-only control-label" for="textarea"></label>
<textarea class="form-control" id="textarea" name="textarea" rows="3" placeholder="COMMENT"></textarea>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<button id="singlebutton" name="singlebutton" value="Submit" class="btn btn-default sub">Submit</button>
</div>
</div>
</div>
</form>
This is how my insert comment function works:
if(isset($_POST['singlebutton'])){
$postid = $_POST["id"];
$name =$_POST['name'];
$email =$_POST['email'];
$comment=$_POST['textarea'];
$commentdata = array(
'comment_post_ID' => $postid,
'comment_author' => $name,
'comment_author_email' => $email,
'comment_content' => $comment,
'comment_type' => '',
'comment_parent' => 0,
'user_id' => $current_user->ID, //passing current user ID or any predefined as per the demand
);
$comment_id = wp_new_comment( $commentdata );
}
Now my problem is that when i hit submit button it refreshes the page and only insert the comment in database and only in next refresh the comments are being displayed in mu page. i have tried using script for window load but that dosent seem to do the work. what can i do ?
<form class="reply-form" id="reply-form" method="POST" action="yourclassname.php">
Add the action like mentioned above.....
After the submit code redirect the page to itself.
IN PHP
header('Location: redirectpage.php');exit();
In Javascript
echo "<script>location.href='redirectpage.php';</script>";
//Just Change name To uname so ‘name’ is not available for a field name.
<input id="name" name="name" class="form-control" placeholder="NAME" required="" type="text">
To
<input id="name" name="uname" class="form-control" placeholder="NAME" required="" type="text">
Am trying ti set a form in my contact page,where the user can send their feedback to me through mails. The problem here is am receiving the mail,but the page is not refreshing and no alert box comes...help to solve this
My Php code-
<?php
if (isset($_POST['submit']))
{
//print_r($_POST);
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$to='xyz#gmail.com';
//$subject='Send mail using php';
//$message='This mail send using php';
$headers="From: $name";
$mail=mail($to,$name,$email,$message);
if($mail)
{
echo'Mail send successfully';
}
else
{
echo'Mail is not send';
}}
?>
and my form is-
<form name="" action="" method="post" class="wpcf7-form">
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-6 col-lg-6">
<label>Your Name</label><br />
<input type="text" name="name" id="name" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required form-control" placeholder="Your name..." />
</div>
<div class="form-group col-xs-12 col-sm-12 col-md-6 col-lg-6">
<label>Phone</label><br />
<input type="text" id="phone" name="phone" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email form-control" placeholder="Phone number..." />
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-6 col-lg-12">
<label>Email Address</label><br />
<input type="text" id="email" name="email" value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email form-control" placeholder="Email Address..." />
</div>
</div>
<div class="form-group">
<label>Message</label><br />
<textarea name="message" id="message" cols="40" rows="10" class="wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required form-control"></textarea>
</div>
<input type="submit" id="submit" name="submit" value="Send Message" class="wpcf7-form-control wpcf7-submit submit_btn" />
</form>
if you want the message displayed as an alert, you can do:
$mail = mail($to,$subject,$message,$header);
?>
<script>
alert('<?php echo $mail ? "Mail sent successfully" : "Mail was not sent" ?>');
</script>
Refer to PHP mail function
$mail=mail($to,$subject,$message,$header);
If you want to create alert change code as below.
$mail=mail($to,$subject,$message,$header);
if($mail)
{
?>
<script type='text/javascript'>alert("Mail sent");</script>
<?php
}
else
{
?>
<script type='text/javascript'>alert("Mail is not send");</script>
<?php
}
for js alert check below code
if($mail)
{
echo'<script>alert("Mail send successfully");</script>';
}
I am currently deploying a contact form to a website via twitter bootstrap where the form posts to a php script that sends an email to an administrator. The problem is that the form does not submit if the file js/plugins/validator/jquery.form-validator.min.js is used for validation
I can get the form to function and submit an email successfully when I remove the validation file below:
js/plugins/validator/jquery.form-validator.min.js
from the script section at the bottom of the page, but this removes all the validation on the form
<form method="post" action="sendmail.php" class="input-group input-group-lg" id="subscribeForm" >
<input type="text class="form-control" placeholder="Name" id="name" name="name" />
<input type="text" name="company" class="form-control" placeholder="company" /><br />
<input type="text" name="phone" class="form-control" placeholder="phone" /><br />
<input type="email" class="form-control" placeholder="email" id="sEmail" name="email">
<span class="input-group-btn">
<input type="submit" button class="btn bg-primary">Invite Me!</button>
</span></form><!-- /input-group -->
And the content of the sendmail.php
<?php
$name = $_REQUEST['name'];
$company = $_REQUEST['company'];
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'];
$emess = "Name: ".$name."\n";
$emess.= "Company: ".$company."\n";
$emess.= "Email : ".$email."\n";
$emess.= "Phone number: ".$phone."\n";
mail( "send-to-email", "Information Request",
$emess, "From: $email" );
header( "Location: http://www.google.com" );?>
In your code you have
<input type="text class="form-control" placeholder="Name" id="name" name="name" />
^ right there
which you are missing a quote " after text. Throw that in and it should work.
Another thing to note on the php side, use $name = $_POST['name']; instead since you are sending a form via post and $_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE.
Edit: As Fred pointed out on the input button it should be
<input type="submit" class="btn bg-primary" value="Invite Me!" />
Changed the input submit tag
<form method="post" action="sendmail.php" class="input-group input-group-lg" id="subscribeForm" >
<input type="text" class="form-control" placeholder="Name" id="name" name="name" />
<input type="text" name="company" class="form-control" placeholder="company" /><br />
<input type="text" name="phone" class="form-control" placeholder="phone" /><br />
<input type="email" class="form-control" placeholder="email" id="sEmail" name="email">
<span class="input-group-btn">
<input type="submit" value="Invite Me!" />
</span></form>
I'm using bootstrap 3 to build a contact form and I have the following problem: If submitted empty, the form submits to the same page. If i enter values in the boxes, it will redirect to the homepage. This problem only occurs if I'm using a name="something" attribute for the input boxes. Does anyone have any idea why? HTML is this:
<form class="form" method="post" action="http://localhost/contact">
<div class="form-group">
<label for="name">Nume</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Numele tau...">
</div>
<div class="form-group">
<label for="email">Adresa email</label>
<input type="email" name="email" class="form-control" id="email" placeholder="Adresa email...">
</div>
<div class="form-group">
<label for="subject">Subiect</label>
<input type="text" name="subject" class="form-control" id="subject" placeholder="Subiectul mesajului...">
</div>
<div class="form-group">
<label for="message">Mesaj</label>
<textarea class="form-control" name="message" id="message" rows="5" placeholder="Mesajul tau aici..."></textarea>
</div>
<button type="submit" class="btn btn-default">Trimite mesajul!</button>
</form>
You need an action to actually submit the form. The form won't do anything until you add an action to process the form and email it.
<form class="form-horizontal" method="post" action="contactform.php">
I use a variation of this form, http://www.html-form-guide.com/email-form/php-form-to-email.html