简体   繁体   English

如何使用输入(表单)标签html / PhP设置变量js

[英]How to set a variable js with input (form) tag html / PhP

I want to know if i can set a variable when the player finish the him form.我想知道当玩家完成他的表格时我是否可以设置一个变量。 Eg:例如:

 <form> First name:<br> <input type="text" name="firstname" value="Pedro"> <br> Last name:<br> <input type="text" name="lastname" value="Pinto"> <br><br> <input type="submit" value="Submit"> </form>

And when he submit, execute a function on JS, what saves the firstname and lastname to a variable.而他提交的时候,在JS上执行一个函数,什么把firstname和lastname保存到一个变量中。

And i want know in PhP, if it's possible too thanks.如果可能的话,我也想在 PhP 中知道,谢谢。

var username = $("input[name=firstname]").val();
var lastname = $("input[name=lastname]").val();

This is how you get the value of both input field with Jquery.这就是您如何使用 Jquery 获取两个输入字段的值。 The first part gets your input by it's name, .val() gets the value from that input.第一部分通过它的名称获取您的输入, .val() 从该输入获取值。

JS solution: In this solution, the data is processed client side. JS 方案:在这个方案中,数据是在客户端处理的。 Be careful though if you are saving any of this to your server since users can use their browser to manipulate the data in unintended ways.但是,如果您将其中的任何内容保存到您的服务器,请小心,因为用户可以使用他们的浏览器以非预期的方式操作数据。

HTML: HTML:

<form>
  First name:<br>
  <input type="text" id="firstname" value="Pedro">
  <br>
  Last name:<br>
  <input type="text" id="lastname" value="Pinto">
  <br><br>
  <input type="submit" value="Submit" onclick="myFunction()">
</form> 

JS: JS:

function myFunction() {
  var firstname = document.getElementById('firstname').value;
  var lastname = document.getElementById('lastname').value;
}

PHP solution: In this solution, the data is processed server side. PHP解决方案:在这个解决方案中,数据是在服务器端处理的。 Here, you will clear out any JS variables because this causes a new page to load when you do this.在这里,您将清除所有 JS 变量,因为这样做会导致加载新页面。 So, generally, do not do this if you need to continue working on page after saving your variables.因此,通常,如果您需要在保存变量后继续在页面上工作,请不要这样做。 Also, note that the HTML forms use "name" instead of "id" when passing a post.另外,请注意 HTML 表单在传递帖子时使用“name”而不是“id”。

HTML: HTML:

<form action="MYPAGE.php" method="post">
  First name:<br>
  <input type="text" name="firstname" value="Pedro">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Pinto">
  <br><br>
  <input type="submit" value="Submit">
</form> 

PHP: PHP:

<?php
  $firstname = $_POST["firstname"];
  $lastname = $_POST["lastname"];
?>

AJAX Solution: The third way to do it which is sort of a hybrid is to use AJAX. AJAX 解决方案:第三种混合方式是使用 AJAX。 In this solution you use JavaScript to collect the variables, then you POST the data to another .php file without navigating away.在此解决方案中,您使用 JavaScript 收集变量,然后将数据 POST 到另一个 .php 文件,而无需导航。 This way you don't clear out any of your JavaScript variables, and you can write the data to the server.这样您就不会清除任何 JavaScript 变量,并且可以将数据写入服务器。 Then whatever content is generated by your targeted PHP page can be loaded into the page you are already on by inserting it into the element you target with your result.然后,您的目标 PHP 页面生成的任何内容都可以通过将其插入到您的结果目标元素中来加载到您所在的页面中。

NOTE: This sample code uses jQuery.注意:此示例代码使用 jQuery。

HTML: HTML:

<form>
  First name:<br>
  <input type="text" id="firstname" value="Pedro">
  <br>
  Last name:<br>
  <input type="text" id="lastname" value="Pinto">
  <br><br>
  <input type="submit" value="Submit" onclick="myFunction()">
</form> 
<div id="MYTARGETELEMENT"></div>

JS: JS:

function myFunction() {
  var firstname = document.getElementById('firstname').value;
  var lastname = document.getElementById('lastname').value;
  $.ajax({
    type:"POST",
    url: "MYPAGE.php", 
    data: {
      firstname:firstname,
      lastname:lastname 
    },
    success: function(result){
      $("#MYTARGETELEMENT").html(result);
    }
  });
}

PHP: PHP:

<?php
  $firstname = $_POST["firstname"];
  $lastname = $_POST["lastname"];
  echo("Post Received By Server"); 
?>

How to get it in PHP:如何在 PHP 中获取它:

Add a name to the submit button:为提交按钮添加一个名称:

<input type="submit" name="submit" value="Submit">

Then use this code:然后使用此代码:

<?php
if(isset($_POST['submit'])) {
   $firstname = $_POST['firstname'];
   $lastname = $_POST['lastname'];
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM