2016-07-01 5 views
0

Ich habe diesen Code, die ich will, dass $X in Form aktualisiert werden, so dass, wenn ich die +1 Taste drücken 1 zum Formular geht einfach weiter das HinzufügenPHP-Variable auto update Ausfall

<html> 
<head> 
<title>7mada</title> 
</head> 
<body> 
<?php $X = 7 ;?> 
<form method="POST" > 
    <table border="0" width="60%"> 
     <tr> <td width ="30%"> Number: </td><td><input type="test"  name="newname" value="<?php echo $X ;?>" readonly></td></tr> 
    </table> 
    <input name="save" type="submit" value="+1"/> 
</form> 

<?php 
if($_POST){ 
if($_POST['save'] == "+1"){ 
$_POST['newname']++; 
echo $_POST['newname']; 
$X = $_POST['newname']; 
} 
}else{echo "say smth";} 
?> 
</body> 
</html> 

Dank.

Antwort

0

Hey Samy ist dies die Antwort auf Ihre Frage: D: D

Es ignoriert vollständig HTML-Ausgabe, daher können PHP nicht mit Ereignissen interagieren, die Javascript sein sollte.

Sie haben zwei Möglichkeiten:

1-Do it in Javascript, Live-

2-Do it in PHP, erfordert

aktualisieren Wenn Sie es in JavaScript tun möchten, würden Sie Dazu:

<html> 
 
    <body> 
 
     <button onclick="add()">Add</button> 
 
     <div id="showIt"><script type="text/javascript">document.write(x);</script></div> 
 
    </body> 
 
    <head> 
 
     <title></title> 
 
     <script type="text/javascript"> 
 
      var x = 0; 
 
      function add() { 
 
       x++; 
 
       document.getElementById('showIt').innerHTML = x; 
 
      } 
 
     </script> 
 
    </head> 
 
</html>

oder in PHP:

<?php 
 
     session_start(); 
 
     $_SESSION['x'] = ((isset($_SESSION['x'])) ? $_SESSION['x'] : 0); 
 
     if(isset($_GET['add'])){ 
 
     $_SESSION['x']++; 
 
     } 
 
?> 
 
<html> 
 
    <head> 
 
     <title></title> 
 
    </head> 
 
    <body> 
 
     <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="get"> 
 
      <input type="submit" name="add" value="add" /> 
 
     </form> 
 
     <?php 
 
     echo $_SESSION['x']; 
 
     ?> 
 
    </body> 
 

 
</html>

Ich hoffe, das hilft Ihnen,

Mahmoud Elsaadany

+0

danken Ihnen viel das wirklich funktioniert <3 –