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.
Related
I want two forms on my page that use the same script. The first one is sent correctly, but the second is not. I'm very beginner in PHP and I have no idea how to do it. I am trying to solve this, unfortunately without results. I'm using the phpmailer.
First form:
<div id="form-main">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="company">
<input name="company" type="text" class="feedback-input" placeholder="Nazwa firmy" id="company"/>
</p>
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Imię i Nazwisko" id="name"/>
</p>
<p class="phone">
<input name="phone" type="tel" required class="feedback-input" id="phone" placeholder="Telefon"/>
</p>
<p class="email">
<input name="email" type="email" required class="feedback-input" id="email" placeholder="Adres e-mail"/>
</p>
<p class="text">
<textarea name="message" class="feedback-input" id="comment" placeholder="Opis zamówienia"></textarea>
</p>
<button type="submit" class="button-blue">Send</button>
</div>
</form>
</div>
Second form:
<div id="second-form">
<form class="montform" id="reused_form" enctype="multipart/form-data">
<p class="name">
<input name="name" type="text" class="feedback-input" required placeholder="Imię i Nazwisko" id="name"/>
</p>
<p class="phone">
<input name="phone" type="tel" required class="feedback-input" id="phone" placeholder="Telefon"/>
</p>
<button type="submit" class="button-blue">Send</button>
</form>
handler.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','phone','company'])->areRequired()-
>maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
$pp->attachFiles(['image']);
$pp->sendEmailTo('sadowm1990#gmail.com'); // ← Your email here
echo $pp->process($_POST);
First: I am pretty sure both of your forms are sent/submitted without any problems. Your second form misses the div closing tag </div> but that shouldn't make such problems.
By "the first one is sent correctly, but the second is not." I assume you mean that you are not getting any email on the selected address when using the second form. If that is the case then:
I am not sure how your validator functions but it looks like that your problem is the validation process.
Why? : In your php code you are validating name, email, phone, company, message but only your first form have chance to satisfy those because it has those fields. Your second form doesn't have all those fields which have to be validated, so, your validation is going to fail. As consequence you are not going to receive any email (if the validator functions in that way).
In order to make your second form to function on this php code you have either to:
1. differentiate those two forms in your php code and apply the correct validation on them or
2. add missing fields to your second form (you can still make them hidden using html/css, but you need to set their default values and submit them altogether, I don't recommend doing this).
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"
I have been trying to pre-fill the subject input with information generated in another page, but have been having difficulties with it despite reading a lot of resources about it and seeing examples. I have tried a variety of links, including my most recent attempt with http://www.myurl.com/folder/index.php/contactform?subject=test, but even that doesn't work. Anybody have any advice? Also, if you want to test it out before answering, the page experiencing the problem is the contact page of this website. I've removed information from below to make it more general. Thanks in advance for any and all of the help.
<form id="contactform" method="post">
<input name="recipient" type="hidden" value="myemail" />
<input name="subject" type="hidden" value="Contacter" />
<p id="contactname">
<label>Name:</label>
<input name="name" type="text" />
</p>
<p id="contactemail">
<label>Email:</label>
<input name="email" type="text" />
</p>
<p id="title">
<label>Subject:</label>
<input name="title" type="text" />
</p>
<p id="contactmessage">
<label>Message:</label>
<textarea name="message"></textarea>
</p>
<p id="submit">
<input type="button" value="Send" />
</p>
<input name="redirect" type="hidden" value="myredirectpage" />
</form>
Lets say your page URL is some thing like below
http://www.example.net/index.php?var1=Something&var2=10&var3=ok
You can use $_GET to get the values of var1, var2,and var3 from the above url
In index.php use the below code to fetch url data
echo $_GET['var1'] // Something
echo $_GET['var2'] // 10
echo $_GET['var3'] // ok
Go through this link http://php.net/manual/en/reserved.variables.get.php
With php you can do this with a session.
On the other page(not the form) you can do $_SESSION['subject'] = 'your subject';
On the form page you can acces this cookie ( make sure you have started the session on top of the page with session_start():
<p id="title">
<label>Subject:</label>
<input name="title" type="text" value="<?= $_SESSION['subject'] ?>"/>
</p>
I have very little knowledge of php so I went ahead and used one from css-tricks email forms he set up for free use:http://css-tricks.com/nice-and-simple-contact-form/.
However I am not sure how to put it into the website without breaking the php code. I assume I can't simply just take the code that I want, which is
<div id="contact-area">
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
<div style="clear: both;"></div>
<p>Check out a version of this with SPAM protection.</p>
</div>
and put it into my html page. I tried using an iframe to link the html page into my html page for my website and it worked, but is this method ok?
Yes, you can just place that coding in your HTML page as long as all the webpage and css file's are linked correctly.(e.g. In the same folder as this page with the coding you have now.)
Using an i-frame for this is not a good idea as it can really mess up your pages and submitting your form.
I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.
Here is the code on the index.html page where the form is
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
Here is the code for send.php
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database .
I found this tutorial which was kind of helpful -
http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
and this is the form on my unsubscribe.html page.
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank.
So I am guessing I can't have two "post" method maybe??
If someone could guide me in the right direction that would be much appreciate. Thanks.
I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.
Example: in your subscribe.php
you should have :
<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />
in send.php
you must do:
if(!isset($_POST['subscribe'])
{
header('location: subscribe.html');
}
You must use isset for your pages.
If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.
Your delete.php script should be:
<?php
require ('connection.php'); // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";}
?>
your delete.php seems OK to me.
Can add the following to Line 2
echo "";
print_r($_POST);
and post array in comments?