hamza Posted February 23, 2010 Share Posted February 23, 2010 i have ajax page on which i have created a simple one field form which contain name filed.which is going to insert in database. and query is rouuning on p.php you can see p page below this problem is when form values are not going to p.php page. and i am passed p.php as a parameter in onsubmit even present in form tag. please tell me why this is not running as expected and any better way to improve to write this problem solution thanks p.php <?php echo $action = $_GET['action'] switch ($action) { case 'add': add(); default: } function add() { $link = mysql_connect('locahost','root','root'); mysql_select_db('test'); echo $sql = " INSERT INTO 'user'(name) VALUES('". $_GET['name'] ."') "; mysql_query($sql); echo '1'; } ?> ajax.php <script type="text/javascript"> function loadXMLDoc(url) { req = false; // branch for native XMLHttpRequest object if(window.XMLHttpRequest && !(window.ActiveXObject)) { try { req = new XMLHttpRequest(); } catch(e) { req = false; } // branch for IE/Windows ActiveX version } else if(window.ActiveXObject) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } } } if(req) { alert(url); req.onreadystatechange = processReqChange; req.open("GET", url, true); req.send(""); } } function processReqChange() { // only if req shows "loaded" if (req.readyState == 4) { // only if "OK" if (req.status == 200) { div_msg.innerHtml = req.responcetext; // ...processing statements go here... } else { alert("There was a problem retrieving the XML data:\n" + req.statusText); } } } </script> <div id="div_msg"></div> <form method="GET" onsubmit="loadXMLDoc('p.php')"> <input type="text" name="name"> <input type="hidden" name="action" value="add"> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/ Share on other sites More sharing options...
danielgrieve Posted February 24, 2010 Share Posted February 24, 2010 I can't tell whether your AJAX will work or not, but your PHP page won't do what you want. Remember to test your PHP pages themselves before calling them with AJAX, so that you can debug them easier. For example, you could easily go to http://localhost/p.php?action=add&name=Dan That would make sure that the script is going to work, which I bet it doesn't. The reason? You seem to have echo statements dotted around everywhere. You only use echo if you want to output something, you have it on your first line where it isn't needed (you're also missing a semi-colon at the end of this line). Change your code to this and see whether it works: <?php $action = $_GET['action']; switch ($action) { case 'add': add(); default: } function add() { $link = mysql_connect('locahost','root','root'); mysql_select_db('test'); $sql = " INSERT INTO 'user'(name) VALUES('". $_GET['name'] ."') "; mysql_query($sql, $link); echo '1'; } ?> Remember: check the page on its own first rather than calling it via AJAX, that way you will get any errors that PHP produces. Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/#findComment-1017291 Share on other sites More sharing options...
hamza Posted February 24, 2010 Author Share Posted February 24, 2010 I can't tell whether your AJAX will work or not, but your PHP page won't do what you want. Remember to test your PHP pages themselves before calling them with AJAX, so that you can debug them easier. For example, you could easily go to http://localhost/p.php?action=add&name=Dan That would make sure that the script is going to work, which I bet it doesn't. The reason? You seem to have echo statements dotted around everywhere. You only use echo if you want to output something, you have it on your first line where it isn't needed (you're also missing a semi-colon at the end of this line). Change your code to this and see whether it works: <?php $action = $_GET['action']; switch ($action) { case 'add': add(); default: } function add() { $link = mysql_connect('locahost','root','root'); mysql_select_db('test'); $sql = " INSERT INTO 'user'(name) VALUES('". $_GET['name'] ."') "; mysql_query($sql, $link); echo '1'; } ?> Remember: check the page on its own first rather than calling it via AJAX, that way you will get any errors that PHP produces. thanks for yoru attention i checked the php page without ajax ...... its working then but when i apply ajax then its not working with ajax i menually tries it with yoru suggestion give a dummy name in url but nothing happening........ Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/#findComment-1017298 Share on other sites More sharing options...
danielgrieve Posted February 24, 2010 Share Posted February 24, 2010 I'm sorry but you seem to be contradicting yourself. i checked the php page without ajax ...... its working then i menually tries it with yoru suggestion give a dummy name in url but nothing happening........ Does the page work when you view it manually or not? Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/#findComment-1017490 Share on other sites More sharing options...
gamblor01 Posted March 1, 2010 Share Posted March 1, 2010 I am also confused whether or not it works manually but I'm guessing that it does not. I believe the problem is that you are quoting your table name in the $sql string. You need to get rid of the quotes around user. Here is what happens when I try things directly inside of MySQL: mysql> create table user ( -> name VARCHAR(32) -> ); Query OK, 0 rows affected (0.01 sec) mysql> describe user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(32) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> insert into 'user' (name) values ('foo'); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user' (name) values ('foo')' at line 1 mysql> insert into user (name) values ('foo'); Query OK, 1 row affected (0.00 sec) I might also recommend that you make your SQL strings much easier. I would recommend code like this: <?php echo $action = $_GET['action'] switch ($action) { case 'add': add(); default: } function add() { $link = mysql_connect('locahost','root','root'); mysql_select_db('test'); $name = $_GET['name']; $sql = "INSERT INTO user (name) VALUES ('$name')"; mysql_query($sql, $link); echo '1'; } ?> You certainly do not need to do all of that string concatenation that makes your code less readable. I would recommend you try the code above and then do (as danielgrieve suggested): http://localhost/p.php?action=add&name=Dan Now does it work? Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/#findComment-1019964 Share on other sites More sharing options...
Dennis1986 Posted March 1, 2010 Share Posted March 1, 2010 The Javascript and HTML isn't correct either. Here's how you could do it: <script type="text/javascript"> function loadXMLDoc(url) { req = false; url = url + '?action=' + document.getElementByName('action').value + '&name=' + document.getElementByName('name').value; // branch for native XMLHttpRequest object if(window.XMLHttpRequest && !(window.ActiveXObject)) { try { req = new XMLHttpRequest(); } catch(e) { req = false; } // branch for IE/Windows ActiveX version } else if(window.ActiveXObject) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; } } } if(req) { alert(url); req.onreadystatechange = processReqChange; req.open("GET", url, true); req.send(""); return false; } else { return true; } } function processReqChange() { // only if req shows "loaded" if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('div_msg').innerHtml = req.responcetext; // ...processing statements go here... } else { alert("There was a problem retrieving the XML data:\n" + req.statusText); } } } </script> <div id="div_msg"></div> <form method="GET" onsubmit="return loadXMLDoc('p.php');"> <input type="text" name="name"> <input type="hidden" name="action" value="add"> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/#findComment-1019976 Share on other sites More sharing options...
okahontas Posted March 23, 2010 Share Posted March 23, 2010 hi there php masters... im a newbie in distress im creating an ajax dropdown combobox and it works fine in localhost. but when i upload the combobox to my hosting server it cant work the combobox contain 4 .php scripts and 1 .js script. the main function of the combobox is to show the bus fare from one place to other the mysql database contain 3 tables origin, destination and price is it a good idea to show the full script here because it will take a lot of space (and i dont know how to make spoiler ... ) thanks for helping a newbie like me... (sorry for the poor english ) Quote Link to comment https://forums.phpfreaks.com/topic/193086-ajax-problem/#findComment-1030672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.