Мне трудно было вызвать необходимые функции jQuery в php, поэтому я добавил их в javascript, но метод, с которым я знаком (функция успеха), не позволяет php выполнять что-либо, кроме запросов INSERT INTO и SELECT. Как я могу изменить этот скрипт так, чтобы он завершил php, и / или как я мог бы объединить код, чтобы можно было выполнить следующее?
Подтверждает форму (с отдельными правилами для мужчин и женщин)
Если проверка прошла успешно: оба пола: parent.close_field («уведомление»); (в настоящее время работает только в javascript)
Если Пол – Женский:
Если Пол Мужской,
Я использую fancybox2 и этот плагин проверки jQuery http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Javascript
var $custInfo = $("#customer_info"); $(document).ready(function () { var validator = $custInfo.validate({ rules: {...}, messages: {...}, errorLabelContainer: "#messageBox", submitHandler: function () { $custInfo.ajaxSubmit({ success: function () { if ($('input[name=gender][value=female]').is(':checked')) { parent.close_field('notice'); window.location.href = "page1.html"; } else if ($('input[name=gender][value=male]').is(':checked')) { parent.close_field('notice'); parent.$.fancybox.close(); alert("This isn't available yet for men, but we'll send you an invitation as soon as it is"); } } }); } }); $custInfo.find("input[name=gender]").change(function () { if ($(this).val() == "male") { $custInfo.submit(); } }); });
PHP
<?php //Start session and connection to database goes here //Function to sanitize values received from the form goes here $gender = $_POST['gender']; if ($gender==="female" ) { // INSERT information into customer_info table $qry = "INSERT INTO customer_info(fname, lname, gender, zip, email, phone, terms, security_question, security_answer, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[security_question]','$_POST[security_answer]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); if($result) { // Identifies user_id assigned to this account $qry="SELECT * FROM customer_info WHERE user_name='$user_name' AND password='".md5($_POST['password'])."'"; $result=mysql_query($qry); if($result) { if(mysql_num_rows($result) == 1) { session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_USER_ID'] = $member['user_id']; $_SESSION['SESS_USER_NAME'] = $member['user_name']; session_write_close(); // Redirects user to the next page header("location: page1.html"); exit(); }else { //user_name failed header("location: login_failed.html"); exit(); } }else { die("Unable to access your account (Error Message 1)"); } }else { die("Unable to access your account (Error Message 2)"); } } // If Gender is Male else { // Notify us of request via email $sendto = "info@click2fit.com";$userfname = $_POST['fname'];$userlname = $_POST['lname'];$usermail = $_POST['email'];$gender = $_POST['gender'];$subject = "Invite Request - " . ($gender) . " "; // INSERT information into invite_requests table $qry = "INSERT INTO invite_requests(fname, lname, gender, zip, email, phone, terms, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); // Echo message to Men echo "<p><strong>Click2Fit is not yet available for men, but we'll be sure to send an invitation as soon as it is</strong></p>"; // Redirects user - This should be replaced with the function which closes the fancybox iframe header("location: home.html"); exit(); } ?>
-<?php //Start session and connection to database goes here //Function to sanitize values received from the form goes here $gender = $_POST['gender']; if ($gender==="female" ) { // INSERT information into customer_info table $qry = "INSERT INTO customer_info(fname, lname, gender, zip, email, phone, terms, security_question, security_answer, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[security_question]','$_POST[security_answer]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); if($result) { // Identifies user_id assigned to this account $qry="SELECT * FROM customer_info WHERE user_name='$user_name' AND password='".md5($_POST['password'])."'"; $result=mysql_query($qry); if($result) { if(mysql_num_rows($result) == 1) { session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_USER_ID'] = $member['user_id']; $_SESSION['SESS_USER_NAME'] = $member['user_name']; session_write_close(); // Redirects user to the next page header("location: page1.html"); exit(); }else { //user_name failed header("location: login_failed.html"); exit(); } }else { die("Unable to access your account (Error Message 1)"); } }else { die("Unable to access your account (Error Message 2)"); } } // If Gender is Male else { // Notify us of request via email $sendto = "info@click2fit.com";$userfname = $_POST['fname'];$userlname = $_POST['lname'];$usermail = $_POST['email'];$gender = $_POST['gender'];$subject = "Invite Request - " . ($gender) . " "; // INSERT information into invite_requests table $qry = "INSERT INTO invite_requests(fname, lname, gender, zip, email, phone, terms, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); // Echo message to Men echo "<p><strong>Click2Fit is not yet available for men, but we'll be sure to send an invitation as soon as it is</strong></p>"; // Redirects user - This should be replaced with the function which closes the fancybox iframe header("location: home.html"); exit(); } ?>
1<?php //Start session and connection to database goes here //Function to sanitize values received from the form goes here $gender = $_POST['gender']; if ($gender==="female" ) { // INSERT information into customer_info table $qry = "INSERT INTO customer_info(fname, lname, gender, zip, email, phone, terms, security_question, security_answer, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[security_question]','$_POST[security_answer]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); if($result) { // Identifies user_id assigned to this account $qry="SELECT * FROM customer_info WHERE user_name='$user_name' AND password='".md5($_POST['password'])."'"; $result=mysql_query($qry); if($result) { if(mysql_num_rows($result) == 1) { session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_USER_ID'] = $member['user_id']; $_SESSION['SESS_USER_NAME'] = $member['user_name']; session_write_close(); // Redirects user to the next page header("location: page1.html"); exit(); }else { //user_name failed header("location: login_failed.html"); exit(); } }else { die("Unable to access your account (Error Message 1)"); } }else { die("Unable to access your account (Error Message 2)"); } } // If Gender is Male else { // Notify us of request via email $sendto = "info@click2fit.com";$userfname = $_POST['fname'];$userlname = $_POST['lname'];$usermail = $_POST['email'];$gender = $_POST['gender'];$subject = "Invite Request - " . ($gender) . " "; // INSERT information into invite_requests table $qry = "INSERT INTO invite_requests(fname, lname, gender, zip, email, phone, terms, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); // Echo message to Men echo "<p><strong>Click2Fit is not yet available for men, but we'll be sure to send an invitation as soon as it is</strong></p>"; // Redirects user - This should be replaced with the function which closes the fancybox iframe header("location: home.html"); exit(); } ?>
-<?php //Start session and connection to database goes here //Function to sanitize values received from the form goes here $gender = $_POST['gender']; if ($gender==="female" ) { // INSERT information into customer_info table $qry = "INSERT INTO customer_info(fname, lname, gender, zip, email, phone, terms, security_question, security_answer, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[security_question]','$_POST[security_answer]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); if($result) { // Identifies user_id assigned to this account $qry="SELECT * FROM customer_info WHERE user_name='$user_name' AND password='".md5($_POST['password'])."'"; $result=mysql_query($qry); if($result) { if(mysql_num_rows($result) == 1) { session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_USER_ID'] = $member['user_id']; $_SESSION['SESS_USER_NAME'] = $member['user_name']; session_write_close(); // Redirects user to the next page header("location: page1.html"); exit(); }else { //user_name failed header("location: login_failed.html"); exit(); } }else { die("Unable to access your account (Error Message 1)"); } }else { die("Unable to access your account (Error Message 2)"); } } // If Gender is Male else { // Notify us of request via email $sendto = "info@click2fit.com";$userfname = $_POST['fname'];$userlname = $_POST['lname'];$usermail = $_POST['email'];$gender = $_POST['gender'];$subject = "Invite Request - " . ($gender) . " "; // INSERT information into invite_requests table $qry = "INSERT INTO invite_requests(fname, lname, gender, zip, email, phone, terms, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); // Echo message to Men echo "<p><strong>Click2Fit is not yet available for men, but we'll be sure to send an invitation as soon as it is</strong></p>"; // Redirects user - This should be replaced with the function which closes the fancybox iframe header("location: home.html"); exit(); } ?>
1<?php //Start session and connection to database goes here //Function to sanitize values received from the form goes here $gender = $_POST['gender']; if ($gender==="female" ) { // INSERT information into customer_info table $qry = "INSERT INTO customer_info(fname, lname, gender, zip, email, phone, terms, security_question, security_answer, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[security_question]','$_POST[security_answer]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); if($result) { // Identifies user_id assigned to this account $qry="SELECT * FROM customer_info WHERE user_name='$user_name' AND password='".md5($_POST['password'])."'"; $result=mysql_query($qry); if($result) { if(mysql_num_rows($result) == 1) { session_regenerate_id(); $member = mysql_fetch_assoc($result); $_SESSION['SESS_USER_ID'] = $member['user_id']; $_SESSION['SESS_USER_NAME'] = $member['user_name']; session_write_close(); // Redirects user to the next page header("location: page1.html"); exit(); }else { //user_name failed header("location: login_failed.html"); exit(); } }else { die("Unable to access your account (Error Message 1)"); } }else { die("Unable to access your account (Error Message 2)"); } } // If Gender is Male else { // Notify us of request via email $sendto = "info@click2fit.com";$userfname = $_POST['fname'];$userlname = $_POST['lname'];$usermail = $_POST['email'];$gender = $_POST['gender'];$subject = "Invite Request - " . ($gender) . " "; // INSERT information into invite_requests table $qry = "INSERT INTO invite_requests(fname, lname, gender, zip, email, phone, terms, participating_retailers, notify_new_items, notify_promotions, priority1, priority2, priority3, priority4, priority5, gift_privacy, user_name, password, Quickfill) VALUES('$_POST[fname]','$_POST[lname]','$_POST[gender]','$_POST[zip]','$_POST[email]','$_POST[phone]','$_POST[terms]','$_POST[participating_retailers]','$_POST[notify_new_items]','$_POST[notify_promotions]','$_POST[priority1]','$_POST[priority2]','$_POST[priority3]','$_POST[priority4]','$_POST[priority5]','$_POST[gift_privacy]','$user_name','".md5($_POST['password'])."','$_POST[Quickfill]')"; $result = @mysql_query($qry); // Echo message to Men echo "<p><strong>Click2Fit is not yet available for men, but we'll be sure to send an invitation as soon as it is</strong></p>"; // Redirects user - This should be replaced with the function which closes the fancybox iframe header("location: home.html"); exit(); } ?>
Важно понимать, что javascript – это клиентский язык (это означает, что он работает в браузере пользователя), а php – это серверный язык (это означает, что он работает на вашем сервере). Чтобы javascript и php могли взаимодействовать друг с другом, вам понадобится использовать AJAX.
поскольку вы уже используете jQuery, я бы предложил вам проверить их AJAX api . По сути, каждый раз, когда вы хотите вызвать php-функцию из вашего javascript-кода, у вас будет что-то вроде этого:
$.ajax({ type: "POST", /*usually POST, but it can also be GET and some other HTTP requests in certain browsers*/ url: "some.php", /*the url of the php which processes your data*/ data: { name: "John", location: "Boston" } /*the data you want to pass to the server. It will be contained in the $_POST array because of the 'type: "POST"' line above*/ }).done(function( msg ) { alert( "Data Saved: " + msg ); /*'msg' is what server send back to you while the contents of the function are what you do once the server finishes. You can change the variable name, msg, as well as the function contents to suit your needs */ });