从PHP中的html获取价值

时间:2014-08-22 03:07:58

标签: javascript php html

如何从php中的html获取值?如何更改“。$ x”。在php中结果是JavaScript中的var click。目前,它只显示值“.x”。当我点击下一个按钮。

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

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

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

3 个答案:

答案 0 :(得分:1)

你需要使用post / get或ajax调用从客户端发送值(用你的话 - html)到php 你的问题不明确。 如果以下不是您所期望的,请告诉我。我会纠正答案 试试这个:

<form method="GET" action="urPHPFIle.php">
<input type="hidden" value="x_value" name="x" />
<input type="click" value="send" />
</form>
单击$ _GET [&#39; x&#39;]值上方的

将等于x_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);
?>

答案 1 :(得分:0)

进行ajax调用

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

来自PHP文件

$x = $_POST['cnt'];

有很多方法可以做到这一点,比如将它存储在隐藏字段中并发布它。等等。也探索它们

答案 2 :(得分:0)

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

//看看这个     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);
}


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>