为了账号安全,请及时绑定邮箱和手机立即绑定

将变量传递给 php

将变量传递给 php

PHP
肥皂起泡泡 2023-04-15 20:27:50
我需要将变量“x”传递给 php,但我不知道如何传递。(我是 html 的新手。如果您需要更多信息,请告诉我。THX :D )function functionT() {  swal({      title: "For help",      text: "Write your phone number here and we will call you:",      type: "input",      showCancelButton: true,      closeOnConfirm: false,      animation: "slide-from-top",      inputPlaceholder: "ex: 0711342647"    },    function(inputValue) {      if (inputValue === false) return false;      if (inputValue === "") {        swal.showInputError("Try again");        return false      }      x = inputValue;      swal("", "You were added on our list: " + x, "success");    });}
查看完整描述

2 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

我找到了一种工作方式。


function functionT() {

  swal({

  title: "For help",

  text: "Write your phone number here and we will call you:",

  type: "input",

  showCancelButton: true,

  closeOnConfirm: false,

  animation: "slide-from-top",

  inputPlaceholder: "ex: 0711342647"

},

function(inputValue){

  if (inputValue === false) return false;


  if (inputValue === "") {

    swal.showInputError("Try again");

    return false

  }

  x = inputValue;




  swal("", "You were added on our list: " + x, "success");

  phone(x);

});

}

function phone(x){


var xmlhttp = new XMLHttpRequest();

  var xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                res = xmlhttp.responseText;


            }

        };

  xmlhttp.open("GET", "php.php?x="+x, true);

  xmlhttp.send();

和PHP:


<?php

$myfile = fopen("newfile.txt", "a+") or die("Unable to open file!");

$phone_nr = $_GET['x'];

fwrite($myfile, $phone_nr);

fclose($myfile);

?> 

php 将在“newfile.txt”中写入电话号码。


查看完整回答
反对 回复 2023-04-15
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

Javascript/Jquery 服务于客户端,而 PHP 服务于服务器端,这意味着在发布表单/页面之前,您不能将 x 值添加到 php 变量。简单的解决方法是在表单元素中创建一个带有 id myxinputbox 的隐藏输入元素,在其中添加 x 值 -

document.getElementById('myxinputbox').value = x;

然后,当您发布表单时,将 x 添加到 php -

$posted_x = $_POST['myxinputbox'];


查看完整回答
反对 回复 2023-04-15
  • 2 回答
  • 0 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信