我用 wordpress + php + google firebase 创建了一些个人项目。 如果 Google firebase OTP 验证成功,我想执行函数。
这是代码
function devsol_customer_form() { ?>
<form>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="number"><?php esc_html_e( 'Mobile Number', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="tel" class="woocommerce-Input woocommerce-Input--text input-text" name="number" id="number"/>
</p>
<div id="recaptcha-container"></div>
<button class="woocommerce-Button button woocommerce-form-login__submit" type="button" onclick="phoneAuth();">SendCode</button>
</form>
<br/>
<h1>Enter Verification code</h1>
<form>
<input type="text" id="verificationCode" placeholder="Enter verification code" class="woocommerce-Input woocommerce-Input--text input-text">
<button class="woocommerce-Button button woocommerce-form-login__submit" type="button" onclick="codeverify();">Verify code</button>
</form>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#config-web-app -->
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "*****",
authDomain: "*****",
databaseURL: "*****",
projectId: "*****",
storageBucket: "*****",
messagingSenderId: "*****",
appId: "*****"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<script>
window.onload=function () {
render();
};
function render() {
window.recaptchaVerifier=new firebase.auth.RecaptchaVerifier('recaptcha-container');
recaptchaVerifier.render();
}
function phoneAuth() {
//get the number
var number=document.getElementById('number').value;
//phone number authentication function of firebase
//it takes two parameter first one is number,,,second one is recaptcha
firebase.auth().signInWithPhoneNumber(number,window.recaptchaVerifier).then(function (confirmationResult) {
//s is in lowercase
window.confirmationResult=confirmationResult;
coderesult=confirmationResult;
console.log(coderesult);
alert("Message sent");
}).catch(function (error) {
alert(error.message);
});
}
function codeverify() {
var code=document.getElementById('verificationCode').value;
coderesult.confirm(code).then(function (result) {
alert("Successfully registered");
var user=result.user;
console.log(user);
}).catch(function (error) {
alert(error.message);
});
}
</script>
<?php }
add_action('woocommerce_after_customer_login_form', 'devsol_customer_form');
我希望如果用户成功验证 OTP,则在我的 php 文件中调用此函数。我在我的 wordpress 主题中使用 function.php 文件。
function devsol_customer_auth() {
$user_phone = sanitize_text_field( $_POST['number'] );
if ( $user = get_user_by( 'login', $user_phone) ) {
$user_id = $user->ID;
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
if ( $user_role === 'customer') {
if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $user_id ) ) {
wc_set_customer_auth_cookie( $user_id );
}
}
}
}
add_action('init', 'devsol_customer_auth');
有人帮忙
最佳答案
您不能直接从 javascript(JS) 调用 php 函数,因为 JS 像浏览器一样在客户端运行,而您的 php 文件存在于网络服务器上。 为了执行类似的操作,您需要向 php 文件发出请求并在请求中传递参数(可以是 GET 或 POST)。 一个简单的例子就是
创建一个单独的 php 文件,比方说 actions.php,它将被请求命中。
从JS请求文件 例如。
function httpGet(theUrl) { var xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", theUrl, false ); // false for synchronous request xmlHttp.send( null ); return xmlHttp.responseText; } httpGet('www.yourSite.com/actions.php?phone=' + userPhoneNumber);
这应该可以解决您的问题。
关于javascript - 如果javascript条件为真,如何调用php函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59583229/