在php中调用jQuery函数(或修改javascript,以便php运行其余代码) [英] Calling jQuery function in php (or modifying javascript so php runs the remaining code)

查看:70
本文介绍了在php中调用jQuery函数(或修改javascript,以便php运行其余代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用php调用必需的jQuery函数时遇到了困难,因此我将它们添加到了javascript中,但是我熟悉的方法(成功函数)阻止了php执行除INSERT INTO和SELECT查询之外的任何操作.我将如何更改此脚本以完成php,和/或如何合并代码以完成以下工作?

I was having difficulty calling the necessary jQuery functions in php so I added them to the javascript, but the method I’m familiar with (the success function) prevents the php from performing anything other than the INSERT INTO and SELECT queries. How would I change this script so that it completes the php, and/or how would I combine the code so that the following can be accomplished?

验证表单(男人和女人有单独的规则)

Validates form (with separate rules for Men and Women)

如果验证成功:两个性别:parent.close_field('notice'); (当前仅适用于javascript)

If validation is successful: Both Genders: parent.close_field('notice'); (currently only works in javascript)

如果性别是女性:

  • 将信息插入到customer_info表中
  • 标识分配给该帐户的user_id
  • 将用户重定向到下一页(当前同时使用php和javascript)

如果性别是男性:

  • 生成电子邮件通知我该请求
  • 将信息插入vital_requests表
  • 回传给男性的消息(目前两者都有;首选方法是在php中)
  • 关闭Fancybox iframe(当前仅适用于javascript)

我正在使用fancybox2和这个jQuery验证插件 http://bassistance.de/jquery-plugins/jquery-plugin-validation/

I am using fancybox2 and this jQuery validation plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/

JavaScript

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();     
    }
?>

推荐答案

重要的是要了解javascript是一种客户端语言(意味着它在用户的浏览器中运行),而php是一种服务器端语言(这意味着它是一种在您的服务器上运行).为了使javascript和php相互交互,您将需要使用AJAX.

It's important to understand that javascript is a client-side language(meaning it runs in the user's browser) and php is a server-side language(meaning that it runs on your server). In order to get javascript and php to interact with each other, you're going to need to use AJAX.

因为您已经在使用jQuery,所以建议您查看他们的 AJAX api .本质上,每次您想从javascript代码中调用php函数时,都会有类似以下内容:

since you're already using jQuery, I would suggest you check out their AJAX api. Essentially, every time you want to call a php function from within your javascript code, you're going to have something along the lines of this:

$.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 */
});

这篇关于在php中调用jQuery函数(或修改javascript,以便php运行其余代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆