12. april 2010 - 20:21Der er
4 kommentarer og 1 løsning
Opdater kommentar uden at refresh hele siden
Hej , jeg har fundet koden til at opdater kommentar uden at refreshe hele siden, det virker også fint med enkelt input felt der hedder message i form, men jeg vil gerne have en input felt mere der hedder navn i form. Hvordan skal koden ændres?
<html> <head> <script type="text/javascript">
function getMessageResponse(str) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById('response').innerHTML=xmlHttp.responseText; document.myform.message.value = ''; } } var url="new.php"; url=url+"?message="+str; url=url+"&sid="+Math.random(); xmlHttp.open("GET",url,true); xmlHttp.send(null); } </script> </head> <body>
<div id="response" name="response">Response will be placed here on submit.</div>
</body> </html>
//new.php
<?php if(isset($_GET['message'])) {
/* this code is missing all error/sanity checks. Just a quick and dirty * example of inserting a record and echoing the complete table back. */ //Connect $con = mysqli_connect("localhost", "demo", "demodemo", "demo");
//Set the message variable that we will insert into the database. $message = mysqli_real_escape_string($con, $_GET['message']);
//Set up and execute the insert query. $insertquery = "insert into demotbl(descr) values('$message')"; mysqli_query($con, $insertquery);
//Set up and execute the select query to return all the values including //the new one. $selectquery = "select * from demotbl order by demo_pk"; $result = mysqli_query($con, $selectquery);
//echo the results from the select query while ($row = mysqli_fetch_assoc($result)) { echo $row["demo_pk"] . " " . $row["descr"] . "<br />"; } //Free result and close connection. mysqli_free_result($result); mysqli_close($con); } ?>
<div id="response" name="response">Response will be placed here on submit.</div>
</body> </html>
//new.php
<?php if(isset($_GET['message'])) {
/* this code is missing all error/sanity checks. Just a quick and dirty * example of inserting a record and echoing the complete table back. */ //Connect $con = mysqli_connect("localhost", "demo", "demodemo", "demo");
//Set the message variable that we will insert into the database. $message = mysqli_real_escape_string($con, $_GET['message']); $navn = mysqli_real_escape_string($con, $_GET['navn']);
//Set up and execute the insert query. $insertquery = "insert into demotbl(descr,navn) values('$message','$navn')"; mysqli_query($con, $insertquery);
//Set up and execute the select query to return all the values including //the new one. $selectquery = "select * from demotbl order by demo_pk"; $result = mysqli_query($con, $selectquery);
//echo the results from the select query while ($row = mysqli_fetch_assoc($result)) { echo $row["demo_pk"] . " " . $row["descr"] ." " . $row["navn"] . "<br />" ; } //Free result and close connection. mysqli_free_result($result); mysqli_close($con); } ?>
virker ikke, kan ikke trykke submit, men du har fat i noget.Ved at ændre url=url+"&navn="+navn; til url=url+"&navn="+str;, kan jeg trykke submit, men man får navn i begge $_GET['message']; og $_GET['navn'];
Det lyder som om du bare har rettet i din kode. Så vil jeg tror at du mangler noget. Du skal i hvert fald ikke ændre navn til str. For så for du message 2 gange. Kan det tænkes at du mangler at ændre knappen? <input value="Post!" type="submit" onclick="getMessageResponse(document.myform.message.value,document.myform.navn.value);">
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.