Jump to content

BkJim

Members
  • Posts

    16
  • Joined

  • Last visited

Posts posted by BkJim

  1. Hi every one,I am using textarea to insert data, but every time I press <ENTER> key, I get n on display and line doesn't change.Exp : Today. <ENTER> tomorrow.On display : Today.ntomorrow.I also try nl2br($_POST['article']) on PHP file and I get same result.How can I solve this ?Thank's in advance.

     

    index.htm

    <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0"> <tr> <td><textarea name="article" id="article"></textarea></td> </tr> <tr> <td><input name="submit" type="button" onclick="submitArticle();" /></td> </tr></table>

     

    article.js

    function getXMLHttpRequest() { var xhr = null; if (window.XMLHttpRequest || window.ActiveXObject) { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } } else { xhr = new XMLHttpRequest(); } } else { alert("XMLHTTPRequest object cannot be support !"); return null; } return xhr;}

    function submitArticle(){ var xhr = getXMLHttpRequest(); var article = encodeURIComponent(document.getElementById('article').value); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { document.getElementById('article').innerHTML = xhr.responseText; } }; xhr.open("POST", "Article.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("&article="+article);}

     

    article.php

    if (isset($_POST['article']) && !empty($_POST['article'])){ $sql = 'INSERT INTO article VALUES("", "'.$_POST['article'].'")'; mysql_query($sql) or die('Erreur SQL !'.$sql.'<br />'.mysql_error());}

     

  2. justsomeguy, on 25 Sept 2013 - 12:19 PM, said:snapback.png

    You can't use Javascript to do that, because Javascript doesn't have access to read the file that you're trying to upload. In order to perform an upload that simulates ajax functionality, the common solution is to add a hidden iframe to the page, submit the form normally to that iframe (so the page doesn't reload), and put a load handler on the iframe to capture the server's response and send that back to Javascript.

     

    Hi i have the same kind of requirement. I am not able to understand clearly what you are saying. up to adding an hidden iframe is ok. what do u meant by "submit the form normally to that iframe" ? do you mean to say submit the form through the iframe or something else. how can we submit the request to the iframe which is on the same page. can u provide some more explanation. if possible some sample code. It will be so much help full to me. Thanking you so much.

     

     

    Hi, I am looking for same explanation.

    Thank you so much.

  3. Hi every one,I am trying to upload text and image using getXMLHttpRequest, but I have no idea ofthe best way to process. Some help would be apreciated.Thank's.

     

    Html file

    <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0"> <tr> <td><input type="text" name="title" id="title"></td> </tr> <tr> <td><textarea name="article" id="article"></textarea></td> </tr> <tr> <td> <form action="" method="post" enctype="multipart/form-data" name="frmart" id="frmart"> <input name="photo" type="file" id="photo" /> </form> </td> </tr> <tr> <td><input name="submit" type="button" onclick="submitArticle();" /></td> </tr></table>

     

    JavaScript file

    function getXMLHttpRequest() { var xhr = null; if (window.XMLHttpRequest || window.ActiveXObject) { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } } else { xhr = new XMLHttpRequest(); } } else { alert("XMLHTTPRequest object cannot be support !"); return null; } return xhr;}

    function submitArticle(){ var xhr = getXMLHttpRequest(); var article = encodeURIComponent(document.getElementById('article').value); var title = encodeURIComponent(document.getElementById('title').value); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { document.getElementById('article').innerHTML = xhr.responseText; } }; xhr.open("POST", "Article.php", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("&article="+article+"&title="+title);}

     

    PHP file

    if (isset($_POST['article']) && !empty($_POST['article']) && isset($_POST['title'])){ $Picture = basename($_FILES['photo']['name']); $File = "Photos/".$_FILES['photo']['name']; $sql = 'INSERT INTO article VALUES("", "'.$_POST['title'].'", "'.$_POST['article'].'")'; mysql_query($sql) or die('Erreur SQL !'.$sql.'<br />'.mysql_error()); $id = mysql_insert_id(); if ($_FILES["photo"]["size"] > 0) { copy($_FILES['photo']['tmp_name'], $File); $sql_p = 'INSERT INTO picture VALUES("", "'.$id.'", "'.$_FILES['photo']['name'].'", "'.$_FILES['photo']['size'].'", "'.$_FILES['photo']['type'].'", "'.$Picture.'")'; mysql_query($sql_p) or die('Erreur SQL !'.$sql_p.'<br />'.mysql_error()); }}

     

  4. I know de point of this forum man and you probably right about GET but why it did work with no changing id ?

    Now, what about message on the next line.

    xhr.send("id="+id+"&message="+message);

    I guess it need some thing empty like this

    var message = "";

    Ok I will let you know soon.

     

    Thanks.

  5. Ok, you right.

    But don't forget minichat.php is displaying on index.php so this is the line with error in minichat.php

    $response = mysql_query("SELECT * FROM message WHERE (user_id = '".$_POST['id']."')");

    And error message

    ( ! ) Notice: Undefined index: id in C:wampwwwminichatminichat.php on line 14

     

    Look, you have tables and all pages. Why don't you try to see what is going on ?

    That is all I have.

  6. In index.php

    <?phpwhile ($data = mysql_fetch_array($req_mem)) {  echo '<div id="refdiv'.$data['user_id'].'" style="height: 100px; width: 200px; overflow: auto; background-color: #CCCCCC; border: 1px solid #555555;"></div>		<input type="text" id="message'.$data['user_id'].'" style="width: 200px;" />		<input type="button" value="Envoyer" onclick="submitChat('.$data['user_id'].');" /><script type="text/javascript">setInterval("refreshChat('' . $data['user_id'] . '')", 2000);</script>';}?>

    And error message

    ( ! ) Notice: Undefined index: id in C:wampwwwminichatindex.php on line 14

  7. Ok I am trying this

    function refreshChat(id){	var xhr = getXMLHttpRequest();	xhr.onreadystatechange = function() {			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {					document.getElementById('rafdiv'+id).innerHTML = xhr.responseText; // Données textuelles récupérées			}	};	xhr.open("GET", "minichat.php", true);	xhr.send(null);}var mytimer = setInterval(function() {refreshChat(id)}, 2000);

    But it doesn't walk at all.

  8. I am printing the hole page of minichat.php on index.php with div.

    <div id="refdiv'.$data['user_id'].'" style="height: 100px; width: 200px; overflow: auto; background-color: #CCCCCC; border: 1px solid #555555;"></div>

    inside WHILE.

    That is the raison why I am asking for refresh.

     

    How to do that, you have my code.

  9. Hi every one, 1 - My database (minichat) has 2 tables CREATE TABLE IF NOT EXISTS `user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(64) DEFAULT NULL, PRIMARY KEY (`user_id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; INSERT INTO user (user_id, user_name) VALUES ('', 'John');INSERT INTO user (user_id, user_name) VALUES ('', 'Mark');INSERT INTO user (user_id, user_name) VALUES ('', 'Peter');INSERT INTO user (user_id, user_name) VALUES ('', 'Simon'); CREATE TABLE IF NOT EXISTS `message` ( `id` int(11) NOT NULL AUTO_INCREMENT, `my_id` int(6) NOT NULL, `user_id` int(6) NOT NULL, `message` varchar(255) NOT NULL, `timestamp` int(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ; 2 - My application has 3 pages index.php

    <?php$connexion = mysql_connect ("localhost", "root", "") or die (mysql_error ());$base = mysql_select_db ("minichat") or die (mysql_error ());$req_mem = mysql_query('SELECT * FROM user WHERE user_id <> 1 ORDER BY user_id DESC');?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><meta http-equiv="Content-Language" content="fr" /><title>Mini-Chat</title><script type="text/javascript" src="minichat.js"></script></head><body><h1>Mini-Chat</h1><?phpwhile ($data = mysql_fetch_array($req_mem)) {  echo '<div id="refdiv'.$data['user_id'].'" style="height: 100px; width: 200px; overflow: auto; background-color: #CCCCCC; border: 1px solid #555555;"></div>		<input type="text" id="message'.$data['user_id'].'" style="width: 200px;" />		<input type="button" value="Envoyer" onclick="submitChat('.$data['user_id'].');" />';}?></body></html>

    minichat.js

    function getXMLHttpRequest() {	var xhr = null;		if (window.XMLHttpRequest || window.ActiveXObject) {		if (window.ActiveXObject) {			try {				xhr = new ActiveXObject("Msxml2.XMLHTTP");			} catch(e) {				xhr = new ActiveXObject("Microsoft.XMLHTTP");			}		} else {			xhr = new XMLHttpRequest(); 		}	} else {		alert("XMLHTTPRequest...");		return null;	}		return xhr;}function submitChat(id){	var xhr = getXMLHttpRequest();	var message = encodeURIComponent(document.getElementById('message'+id).value);	document.getElementById('message'+id).value = "";		xhr.onreadystatechange = function() {			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {					document.getElementById('refdiv'+id).innerHTML = xhr.responseText; 			}	};		xhr.open("POST", "minichat.php", true);	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	xhr.send("id="+id+"&message="+message);}

    minichat.php

    <?phpheader("Content-Type: text/html; charset=iso-8859-1");mysql_connect("localhost", "root", "");mysql_select_db("minichat");if (isset($_POST['message'])) {    if (!empty($_POST['message']))     {		$message = mysql_real_escape_string(utf8_decode($_POST['message']));        mysql_query("INSERT INTO message(my_id, user_id, message , timestamp) VALUES(1, '".$_POST['id']."', '$message', '".time()."')");    }}$response = mysql_query("SELECT * FROM message WHERE (user_id = '".$_POST['id']."')");while($val = mysql_fetch_array($response)){	echo '<p><strong">'.htmlentities(stripslashes($val['user_id'])).'</strong> à '.date('H:i:s',$val['timestamp']).' : '. htmlentities(stripslashes($val['message'])) .'</p>';}mysql_close();?>

    3 - My question Can you please tell me how to refresh refdiv in WHILE ('refdiv'+id) every 2 seconds (2000 milliseconds)Because I need unique div associate with user_id. Thank you in advance.

×
×
  • Create New...