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

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

@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

 

 

@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

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>

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.

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.

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. =/

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.