Avoid scrolling up on submit form php - php

I am working on a simple contact form.
It uses PHP code to show a "Thank you" message after the submit.
The problem there is about it scrolls up as soon as you submit the form.
PHP CODE:
<?php
session_name("fancyform");
session_start();
$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];
if(isset($_SESSION['sent']))
{
$success='<h1>Thank you!</h1>';
unset($_SESSION['sent']);
}
?>
FORM CODE (SECTION):
<section id="revealAnim" class="page5">
<div class="page_container">
<div class="icono red"></div>
<p class="titulo_cabecera">Necesito soluciones!</p>
<p class="subtitulo_cabecera">Rellena el formulario y estaremos en contacto en menos de 24h</p>
<form class="demo-form" name ="demo-form" data-parsley-validate method="post" action="submit.php" >
<ul>
<li class="js-hide-label">
<label for="name">Nombre:</label>
<input type="text" placeholder="Nombre" id="name" name="name" data-parsley-trigger="change" required tabindex="1" autocomplete="off" value="<?php echo (isset($_SESSION['post']['name']) ? $_SESSION['post']['name'] : ''); ?>" >
</li>
<li class="js-hide-label">
<label for="email">Your Email:</label>
<input type="email" placeholder="Your Email" id="email" data-parsley-trigger="change" name="email" autocomplete="off" required tabindex="2" value="<?php echo (isset($_SESSION['post']['email']) ? $_SESSION['post']['email'] : ''); ?>">
</li>
<li class="js-hide-label">
<label for="message">Message:</label>
<textarea placeholder="Message…" id="message" name="message" tabindex="3" required data-parsley-trigger="keyup" textarea id="message" data-parsley-minlength="20" data-parsley-maxlength="100" data-parsley-minlength-message = "Come on! You need to enter at least a 20 caracters long comment.." data-parsley-validation-threshold="10"><?php echo (isset($_SESSION['post']['message']) ? $_SESSION['post']['message'] : ''); ?></textarea>
</li>
<input class="btn btn-default"type="submit" name="button" id="button" value="Submit" />
</ul>
<?php echo isset($success)?$success:''; ?>
</form>
</section>
Should I work on a Javascript form to avoid the problem? Or there is any solution working on the PHP code?.
Otherwise I am working with ONE PAGE SCROLL so the scrolls breaks my usability on the 90% of users, since they have to scroll back to the bottom page (they are 5) to see if the form has been submited.
Thank you

You could either use AJAX/Javascript like you have suggested, or you could redirect the visitor to an id tag.
http://example.com/index.php#contact
So the body will automatically be scrolled to the contact form after submit.

By default a web page reloads when a form is submitted. This is how PHP works.
When a user clicks on the submit button an http request is sent to the server and thus requesting new data, so the page must be reloaded to preview response data.
The way to avoid this is by using AJAX
there is a good example found on this link: Submit A Form Without Page Refresh using jQuery

Related

Ajax call on submit refreshing page

Problem
My page seems to be defaulting to my PHP code instead of being handled asynchronously via ajax. As of now, my page just reloads as a blank screen, the input array successfully got passed over to the PHP, which is comforting, but I seem to just be doing something wrong with the ajax.
What I've tried
After viewing this link and this link, I still cant seem to get this figured out.
Current Standing
Here is the HTML:
<form class="cmxform" id="commentForm" method="" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="spinner">How much do you love science? (optional)</label>
<input id="spinner" name="spinner">
</p>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" minlength="2" type="text" required>
</p>
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url">
</p>
<p>
<label for="aoi">Area of Interest (optional)</label>
<input id="aoi" type="text" name="aoi">
</p>
<p>
<label for="currprobs">Current Problems (optional)</label>
<input id="currprobs" type="text" name="currprobs">
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input id="launch" type="submit" value="Submit">
</p>
</fieldset>
</form>
<div id="results"></div>
Here is the javascript:
$(document).ready(function(){
$("#commentForm").submit(function(event){
/*
var formData = {
'name' : $('input[name=name]').val(),
'email' : $('input[name=email]').val(),
};
*/
alert("SUCCESS?");
});
});
PHP code to return a div after the user has been validated:
<?php
$username = isset($_POST['name'])? trim($_POST['name']):'';
$email= isset($_POST['email'])? trim($_POST['email']):'';
echo '<div id=dynamicDiv>
<p>Hello, '.$username.'!</p>
<p>We look forward to contacting you at, '.$email.'</p>
</div>';
?>
Feedback
Any thoughts are appreciated, even intellectual conversation. Code snippets are idea, but I appreciate anything the community is able
Edit 1
I motified the javascript and php to reflect knowledgable input and best practice for PHP and AJAX calls, however the issue still remains.
Edit 2
My goal now is to just get an alert() statement working inside of my javascript, ajax will be the next step.
I think problem is here
$("#results").html = html_i;
You try change to
$("#results").html(html_i);
And in php you should check name and email
$username = isset($_POST['name'])? trim($_POST['name']):'';
$email= isset($_POST['email'])? trim($_POST['email']):'';
You should change input type="submit" to input type="button" and call Ajax on onclick function, Its resolve your problem because submit button submit form and Ajax call pending not reached to 400 state.

PHP Form Redirect to error link

Using HTML form with action = "www.google.com", but it redirect to link including localhost/www.google.com . My example code is for your reference. Please anyone help me.
My Forms as follows :
<form method="POST" action="www.google.com">
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">Email:</label>
<input type="email" name="email" id="email">
</p>
<p>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments"></textarea>
</p>
<p>
<input type="submit" name="send" id="send" value="Send Comments">
</p>
</form>
But, the file when click send button, it redirects to
" http://localhost/basic/www.google.com" error page. Can any one suggest me to resolve the issue.
It is not allowed to send POST request directly to google.com. Why do you want to do this? In the action argument you should have own script which can process request from your form.
Replace your form action
From
action="www.google.com"
To actual link
action="https://www.google.com"

Scroll down to contact form at bottom of page when form is submitted with errors

What I'm trying to accomplish is when a user tries to submit the form with errors and the error messages are added/page reloads I need the page to automatically scroll down to the bottom of the page to the contact form location.
I currently have the form within my footer.php and I'm including it in on each page like this: <?php include('includes/footer.php') ?>
here is my html for the form:
<form method="post" action="#">
<input name="name" placeholder="Name*" type="text" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span>
<input name="email" placeholder="Email*" type="email" value="<?php echo htmlspecialchars($email);?>">
<span class="error"><?php echo $emailErr;?></span>
<input name="phone" placeholder="Phone #" type="tel" />
<textarea name="message" placeholder="Message*"><?php echo $message;?></textarea>
<span class="error"><?php echo $messageErr;?></span>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
EDIT -
Based on the answer below this is what fixed my problem...
<form method="post" action="#footer">
//everything here
</form>
While redirect to the page with error msg, add #id_of_form_or_div in your url.
Add id to your form or div which containing the form. If you add their id in url, then page will move to that particular section.
For example, You have id for footer as footer. Then you url will be
www.domain.com/index.php#footer
You can also print a javascript variable to know if there are errors, then with javascript scroll to the form.
something like:
var erros = "<?php echo $errors ?>";
<script>
if (errors) {
document.getElementById("form_id").scrollIntoView();
}
</script>

PHP form without opening a new window

Good day/evening. I have this sort of form in HTML:
<form action="scripts/form-testimonial.php" class="form" method="post">
<ul>
<li><label for="name">Name</label> <input class="required" id="namef3" name="name" placeholder="Name" type="text"></li>
<li><label for="email">E-mail</label> <input class="required" id="emailf3" name="email" placeholder="E-mail" type="text"></li>
<li><label for="company">Company</label> <input id="companyf3" name="company" placeholder="Company (if applicable)" type="text"></li>
<li><label for="msg">Message</label> <textarea class="required" cols="43" id="msgf3" name="msg" placeholder="Your feedback" rows="8"></textarea></li>
</ul>
<button class="submit" type="submit">Send your feedback</button>
</form>
and this sort of php sript for this form as a separate file:
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
header("Location: ../error.html");
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
header("Location: ../msg_sent_feedback.html");
}
?>
I also have a validator for this form, but this is not important. What is more important here is the way how this form works. Basically - when you click submit button - it goes to a separate php file, process the form and then - from what is already there - goes to msg_sent_feedback.html file. What I want to do is to NOT opens a separate file but just display a message in the same basic html file where the actual form exists. A message e.g. "Message sent".
Another thing - if I already have a validation script - do I need to have the first "if" in this form? or can I remove it totally? the one which redirects to error.html. Or can I remove these 4 lines? That line - I guess - is responsible for errors in the form, but as I mentioned, I have a validation script. I don't really want to change html. is it possible to modify the php script to work within the html document, as instructed above? thanks
Here is an Jquery ajax example that will help you.
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
echo "<h1>Error sending e-mail";
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
echo "<h1>Email sent successfully</h1>";
}
?>
Html :
<!Doctype html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function submitmyform(){
data=$('#myform').serialize();
$.ajax({
url: "myphpfile.php",
type:'POST',
data:data,
async:false,
dataType:'html',
success: function(msg){
$('#response_goes_here').html(msg);
}
});
}
</script>
<form action="" id="myform">
<ul>
<li> <label for="name">Name</label> <input id="namef3" name="name" type="text" placeholder="Name" class="required"/>
</li>
<li> <label for="email">E-mail</label> <input id="emailf3" name="email" type="text" placeholder="E-mail" class="required"/>
</li>
<li> <label for="company">Company</label> <input id="companyf3" name="company" type="text" placeholder="Company (if applicable)" />
</li>
<li> <label for="msg">Message</label> <textarea id="msgf3" name="msg" cols="43" rows="8" placeholder="Your feedback" class="required"></textarea>
</li>
</ul>
<button type="button" class="button" onclick="submitmyform();">Send your feedback</button>
</form>
<div id="response_goes_here"></div>

PHP not working correctly in WordPress

I have made a plain PHP widget to be displayed on a WordPress sidebar. I have successfully made the widget post the data I am hoping to have filled in on the consecutive page. However where it is supposed to be will not fill in, instead it fills in with "<?php echo $_GET[" then after the text box " />". I am hoping that the email first submitted will fill in on the form on the next page. The code that I have for the registration form is part of a greater widget and looks like the following:
<p class="form-email'.$errorVar.'">
<label for="email">'. __('E-mail', 'profilebuilder') .$errorMark.'</label>
<input class="text-input" name="email" type="text" id="email" value="<?php echo $_GET["email"]; ?>" />
</p><!-- .form-email -->';
Here is a link to the page: http://universityoutfitters.com/testphp/ — the widget is on the bottom left hand side panel.
Additional information:
The code for the widget is as follows:
<form action="http://universityoutfitters.com/sign-up/" method="post">
Please submit your email address
Email: <input type="text" name="email" />
<input type="submit" />
</form>
This line should be:
<label for="email"><?php echo __('E-mail', 'profilebuilder') $errorMark ;?></label>
As the comments told above, you have to wrap it correctly with PHP tags
<?php
echo '<p class="form-email'.$errorVar.'">
<label for="email">'. __('E-mail', 'profilebuilder') .$errorMark.'</label>
<input class="text-input" name="email" type="text" id="email" value="'.$_GET["email"].'" />
</p><!-- .form-email -->';
?>

Categories