hitokirikensan Posted January 22, 2011 Share Posted January 22, 2011 Hi all, I have a regular comment section and i want the submit button to call a mysql_query function in comment.class.php so <form method="post" action="../php/comment.class.php?action=addcomment" name="commentform" id="commentform" onsubmit="validateForm()"> <label for "body">Your Comment</label> <div><textarea cols="20" rows="4" name="body" id="body"></textarea></div> <input type="submit" id="submit" value="Submit"/> and my comment.class.php is... function addcomment($body){ $body=mysql_real_escape_string($_POST['body']); if($body!=''){ mysql_query("INSERT INTO comment(uid,screenname,body) VALUES ('$_SESSION[uid]','$_SESSION[screenname]','$body')") or die('lame'); } header("location:../index.php"); } but when I click submit, i'm taken to a blank page with the URL: http://test/php/comment.class.php?action=addcomment any tips? and is there a better alternative to header? Thanks Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/ Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 http://www.phpfreaks.com/forums/php-coding-help/make-a-link-execute-a-script/msg1516757/#msg1516757 Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163480 Share on other sites More sharing options...
hitokirikensan Posted January 22, 2011 Author Share Posted January 22, 2011 So i wrote if(isset($_GET['action']) && $_GET['action']=='addcomment' && isset($_POST['body'])) { addcomment($body); } but i'm not sure where to put it. in the php file? i tried putting it before and after the function addcomment()... and i'm getting alot of weird errors like Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user that i didn't see before Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163493 Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Looks like the function is executing then. You don't have an open/valid database connection though. Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163500 Share on other sites More sharing options...
codefossa Posted January 22, 2011 Share Posted January 22, 2011 You could always use JQuery and a separate page for the function you wish to call on. That avoids the need to refresh the page, you can just add the content to a div. Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163506 Share on other sites More sharing options...
hitokirikensan Posted January 22, 2011 Author Share Posted January 22, 2011 @thorpe i was able to connect before i added the $_GET. (also have a include "php/connect.php"; at the top of index) i don't really get the logic of how the new line i just added works. i did include "php/comment.class.php"; at the top of index so then when the addcomment function is called, it automatically checks the isset? so the isset can be placed anywhere in my comment.class.php? @kira do you mean jquery+ajax? i haven't learned ajax yet =p Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163518 Share on other sites More sharing options...
hitokirikensan Posted January 22, 2011 Author Share Posted January 22, 2011 @thorpe Oh i was able to get it by calling the connect.php again but i don't understand why it was lost. it was called in index.php, but after the action=....comment.class.php the include 'connect.php'; no longer works? @kira do you mean jquery+ajax? i haven't learned ajax yet =p Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163523 Share on other sites More sharing options...
codefossa Posted January 22, 2011 Share Posted January 22, 2011 Well, luckily for you I have it already which I reuse all the time? Just needs simple edits. <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script> <style> label.error { width: 250px; display: inline; color: red;} </style> <script type="text/javascript"> function ahah(url, target) { if (window.XMLHttpRequest) { req = new XMLHttpRequest(); } else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); } if (req != undefined) { req.onreadystatechange = function() {ahahDone(url, target);}; req.open("GET", url, true); req.send(""); } } function ahahDone(url, target) { if (req.readyState == 4) { if (req.status == 200) { document.getElementById(target).innerHTML = req.responseText; } else { document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText; } } } function load(name, div) { ahah(name,div); return false; } </script> Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163525 Share on other sites More sharing options...
codefossa Posted January 22, 2011 Share Posted January 22, 2011 I guess I should say that I didn't create that, just edited someone elses as I'm not great with JQuery, but something that simple I could have easily handled, but that was a long time ago before I learned it. Either way, knowing it or not, I copy and paste. I forget where it originally came from. Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163526 Share on other sites More sharing options...
hitokirikensan Posted January 22, 2011 Author Share Posted January 22, 2011 how is it saved in mysql though? Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163529 Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Kira, the code you posted doesn't even use jQuery I'm afraid. Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163530 Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 i did include "php/comment.class.php"; at the top of index so then when the addcomment function is called, it automatically checks the isset? Your connection needs to be made in the file that calls mysql_query(). Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163531 Share on other sites More sharing options...
codefossa Posted January 22, 2011 Share Posted January 22, 2011 Haha, I took out the actual JQuery part of it when I saved it. With what's there you can use a button and onClick="load()" but if you need to post data to the function I can get post up the JQuery usage. Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163533 Share on other sites More sharing options...
trq Posted January 22, 2011 Share Posted January 22, 2011 Haha, I took out the actual JQuery part of it when I saved it. With what's there you can use a button and onClick="load()" but if you need to post data to the function I can get post up the JQuery usage. This isn't the Ajax board, nor is it helping the op solve there problem. Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163534 Share on other sites More sharing options...
codefossa Posted January 22, 2011 Share Posted January 22, 2011 Haha, I took out the actual JQuery part of it when I saved it. With what's there you can use a button and onClick="load()" but if you need to post data to the function I can get post up the JQuery usage. This isn't the Ajax board, nor is it helping the op solve there problem. I guess I should'a read the post rather than just going by the title. =/ Link to comment https://forums.phpfreaks.com/topic/225289-calling-a-php-function-with-submit-button/#findComment-1163540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.