Aufruf Ich weiß nicht, warum, aber JavaScript kann meine Eingabe nicht fangen:Kann nicht Eingang ID fangen, wenn JS-Funktion von PHP
Mensaje: <input id="mensaje" type="text" name="mensaje" value="" style="width:30%;" placeholder="Escribe un mensaje a enviar...."/>
gebaut, wenn ich so unterbreitet die PHP es erkennen und nennen JS-Funktion:
function ClearFields() {
var e = document.getElementById("mensaje");
e.value='';
}
Mein Formular nie die Seite neu laden und ich kann einen Text ohne Neuladen einreichen. Das Problem ist, dass der Wert von Textfeld immer beibehalten wird. Deshalb versuche ich, JavaScript zu verwenden, um dieses Textfeld zu löschen, wenn es eingereicht wird.
ich diesen Fehler in JS-Konsole:
(index):24 Uncaught TypeError: Cannot set property 'value' of null(…)
Voll Code:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Tangerine">
<title>Chat de SL</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<script type="text/javascript">
$(document).ready(function() {
rsdfsdfsd();
asd();
});
function rsdfsdfsd() {
$('#nax').load('./funcionmensaje.php');
setTimeout(rsdfsdfsd, 200);
}
function ClearFields() {
var e = document.getElementById("mensaje");
e.value = '';
}
</script>
<script>
var contador = 0;
function asd() {
if (contador < 2) {
contador++;
var elem = document.getElementById('nax');
elem.scrollTop = elem.scrollHeight;
setTimeout(asd, 100);
}
}
</script>
<script>
</script>
</head>
<body>
<?php
if(isset($_POST['subname'])) {
echo '<script>',
'ClearFields();',
'</script>'
;
if ($_POST['mensaje'] == "") {
}
else {
file_put_contents('mensajeaenviar', $_POST['mensaje']);
file_put_contents('var', '0');
$fp = fopen('mensaje.php', 'a');
$mensaje = $_POST['mensaje'].PHP_EOL;
fwrite($fp, 'Tú: '.$mensaje .'<br>');
}
}
?>
<fieldset>
<font color="#c50000">Lugar:</font> Attached |
<font color="#c50000">Avatares cercanos:</font>
</fieldset>
<fieldset>
<legend>Chat</legend>
<td>
<div id="nax" style="height:70%; overflow:auto">
</div>
</td>
</fieldset>
<style>
nax {
font-family: 'Open Sans', serif;
font-size: 18px;
}
fieldset {
font-family: sans-serif;
border-radius: 10px;
background: #eee;
margin: 20px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, .3);
position: relative;
border: 2px groove threedface;
}
legend {
position: absolute;
top: -2px;
background: #fff;
padding: 10px 20px;
border: 2px groove threedface;
border-top: none;
box-shadow: inset 0 -5px 5px rgba(0, 0, 0, .2);
text-shadow: 1px 1px 2px rgba(0, 0, 0, .3);
font-weight: bold;
}
legend span {
display: block;
margin: -20px;
background: #fff;
padding: 0px 20px;
}
footer {
font-family: sans-serif;
margin-top: 2%;
background: #ddd;
border-radius: 5px;
padding: 15px;
}
iframe {
display: none;
}
</style>
<fieldset style="margin-top:10px;height:3%;">
<form id="formulario" method="post" target="tar">
Mensaje: <input id="mensaje" type="text" name="mensaje" value="" style="width:30%;" placeholder="Escribe un mensaje a enviar...." />
<input type='submit' value='Enviar' name='subname' />
</form>
</fieldset>
<iframe name="tar" src="localhost/a.php">
</iframe>
</body>
<footer>EnriqueGF Utilities</footer>
</html>
Einige Tipps: Verwenden Sie aussagekräftige Namen, entfernen Sie unbenutzte Tags und Codepfade (wie der leere 'if'-Zweig), vermeiden Sie Inline-Stile wann immer möglich, JavaScript kann PHP-Funktionen nicht aufrufen und umgekehrt (ohne andere Mittel, wie ein API). –