Jump to content

calling a php function with submit button


hitokirikensan

Recommended Posts

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
Share on other sites

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
Share on other sites

@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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.