简体   繁体   English

从PHP中的HTML获取价值

[英]Getting value from html in php

How can I get the value from the html in php? 如何从php中的html获取值? How can I change the ".$x." 如何更改“。$ x”。 in php to result which is var click in the JavaScript. 在php中,结果为var,然后单击JavaScript。 Currently, it only display the value ".x." 当前,它仅显示值“ .x”。 when I click the next button. 当我单击下一步按钮时。

php file php文件

<?php

$con=mysqli_connect("localhost","root","password","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$x = 1;

$sql="select * from sentence where id = ".$x." limit 10";

if ($result=mysqli_query($con,$sql))
{
 // Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$output = $row[1];
}
 // Free result set
 mysqli_free_result($result);
}

mysqli_close($con);
?>

Javascript 使用Javascript

function next() {

if(typeof(Storage) !== "undefined") {
    if (sessionStorage.clickcount) {
        sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
    } else {
        sessionStorage.clickcount = 1;
    }
    document.getElementById("result").innerHTML =  sessionStorage.clickcount;
} else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support web     
storage...";
}

}
var click = sessionStorage.clickcount;

function nextnext(){
alert(click);
}

Html HTML

 <center><button onclick='next()'>NEXT</button></center>
 <div id="result"></div>

you need to either use a post/get or ajax call to send value from client(in ur words - html) to php Your question is not clear. 您需要使用post / get或ajax调用来将值从客户端(以您的单词-html)发送到php,您的问题尚不清楚。 If below is not what you are expecting let me know. 如果不是您所期望的,请告诉我。 I will correct the answer try this: 我将更正答案,请尝试以下操作:

<form method="GET" action="urPHPFIle.php">
<input type="hidden" value="x_value" name="x" />
<input type="click" value="send" />
</form>

on clicking above the $_GET['x'] value will be equal to x_Value or the value you enter in value attribute of input 单击$ _GET ['x']值上方的值将等于x_Value或您在input的value属性中输入的值

<?php

$con=mysqli_connect("localhost","root","password","DB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$x = 1;

$sql="select * from sentence where id = ".$_GET['x']." limit 10";

if ($result=mysqli_query($con,$sql))
{
 // Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$output = $row[1];
}
 // Free result set
 mysqli_free_result($result);
}

mysqli_close($con);
?>

do an ajax call 做一个ajax电话

$.ajax({
   url: "filename.php",
   type: "POST",
   data: { cnt : count},
}).done(function( msg ) {
    // to do here after ajax call 
});

from PHP file 从PHP文件

$x = $_POST['cnt'];

there are many ways to do it,.. like storing it in hidden field and posting it. 有很多方法可以执行此操作,例如将其存储在隐藏字段中并发布。 etc.. explore them also 等..也探索他们

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Scramble Game</title>
<script type="text/javascript" src="https://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js">        
</script>
</head>

<body>
<header>
<h1>Drag in order to create a sentence</h1>
<div id="cardContainer" draggable="true"></div>


<?php

$con=mysqli_connect("localhost","root","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
$x = 1;

$sql="select * from sentence where id = ".$x." limit 10";

if ($result=mysqli_query($con,$sql))
 {
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$output = $row[1];
}
 // Free result set
 mysqli_free_result($result);
}

mysqli_close($con);
?>

<div id="adiv" style="font:24px bold; display:block"><p id="demo"></p></div>

<script>

js_var = "<?php echo $output ?>";
var c = ['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10'];
var div_id = ['id1','id2','id3','id4','id5','id6','id7','id8','id9','id10'];
var box_id = ['box1','box2','box3','box4','box5','box6','box7','box8','box9','box10'];
var original = js_var.split("#");
var balls90= js_var.split("#");

var org_txt="";
for (i=0;i<balls90.length;i++){
org_txt=org_txt.concat(original[i]);
}

document.getElementById("demo").innerHTML =  org_txt;


setTimeout(fade_out, 5000);

function fade_out() {
$("#adiv").fadeOut().empty();
}

//look at this function next() { //看看这个函数next(){

if(typeof(Storage) !== "undefined") {
    if (sessionStorage.clickcount) {
        sessionStorage.clickcount = Number(sessionStorage.clickcount)+1;
    } else {
        sessionStorage.clickcount = 1;
    }
    document.getElementById("result").innerHTML =  sessionStorage.clickcount;
  } else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support web 
  storage...";
}

}
var click = sessionStorage.clickcount;


function nextnext(){
alert(click);
}


function getNumbers() {
var player1 = new Array();
balls90.sort(function() {
    return Math.random() - .25;
    document.getElementById("btn_shuffle").onclick = function(){displayDate()};
});


 for (var i = 0; i < balls90.length; i++) {
     document.writeln('<div id='+box_id[i]+' class=box droppable=true ondrop=drop(event)     
ondragover=allowDrop(event)></div>');
 }


for (var i = 0; i < balls90.length; i++) {
    player1.push(balls90[i]);

    document.writeln('<div id='+div_id[i]+' draggable=true droppable=true     
ondragstart=drag(event)>'+balls90[i]+'</div>');
  }
}

getNumbers();


function dragWord(dragEvent){
dragEvent.dataTransfer.setData("text/html",     
dragEvent.target.textContent+"|"+dragEvent.target.parentNode.id);
}


function dropWord(dropEvent){
var dropData = dropEvent.dataTransfer.getData("text/html");
var dropItems = dropData.split("|");
var prevElem = document.getElementById(dropItems[1]);
prevElem.getElementsByTagName("div")[0].textContent = dropEvent.target.textContent;
dropEvent.target.textContent = dropItems[0];
dropEvent.preventDefault();
}

function allowDrop(ev) {
ev.preventDefault();
}  


function drag(ev) {
    ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("Text");
    ev.target.appendChild(document.getElementById(data));
var match = 0;
//if(){//if 4 boxes are not empty

 var result = true;
 for (var i=0; i<original.length; ++i) {
 if (original[i] !== $("#box"+(i+1)).text()) {
 result = false;
 break;
}
}

if(result) {
alert("correct");

} 
}

</script>
<center><button onclick='next()'>NEXT</button></center>
<div id="result"></div>

</body>
</html>

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

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