0

How to check textarea value is submitted or not on insert page?

if (isset($share)){
//do stuff
}

or anything else?

I'm using click function #share instead form submit.
How to check #share is clicked, on insert.php page?
config.php

$host="localhost";
$user="";
$password="";
$db="";

$link = mysql_connect("$host", "$user", "$password");
$con=mysql_select_db("$db")or die(mysql_error());

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Wall Post</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.js">  </script>

<script type="text/javascript">
$(document).ready(function(){		
  $('#share').click(function(){			
	var status=$(".input").val();
	var DATA = 'status=' + status;
	$.ajax({
	type: "POST",
	url: "insert.php",
	data: DATA,
	success: function() {
	  //display message back to user here
	}
	 });	  
	  return false;	
	});
});
</script>


</head>

<body>
 <div class="status">
    <textarea class="input" name="status" id="content" >So whats on your mind?</textarea>
</div>

<div class="button_outside" id="share">
    <div class="button_inside">Share</div>
</div>
 

</body>
</html>

insert.php

<?php
include("config.php");

$status=$_POST["status"];

$query = "INSERT INTO streams (message) VALUES ('$status')";
mysql_query($query) or die(mysql_error());

?>
5
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Biiim
0

Add another parameter to the data: part in your jquery function. There is no other way to know if user has clicked on Share since this is not a form element.

var DATA = 'status=' + status + '&share=true';
0

try that:

if (isset($_POST['share'])){ 
//do stuff 
}

Aren't you in the middle of another thread with this issue?

0

Not working

try that:

if (isset($_POST['share'])){ 
//do stuff 
}

Aren't you in the middle of another thread with this issue?

0

I want to stop user go directly to following and insert empty records to database

http://localhost/code/insert.php

So I want to check submitted or not on insert.php before insert any record.

Add another parameter to the data: part in your jquery function. There is no other way to know if user has clicked on Share since this is not a form element.

var DATA = 'status=' + status + '&share=true';
0

I think this small change might work for you:

if (isset($_POST['status'])){ 
//do stuff 
}

The textarea wouldn't be populated unless the submit button was entered ... probably not the ideal solution, but should work

0

I know what you're trying to do. The post var I showed you is the std way (imo). You must have a mistake elsewhere.

0

Also the .input change to #content and use .html() or text() can't remember which

0

your insert.php page:

<?php
include("config.php");
 
if(isset($_POST['status']){
	//field is set
	$status = $_POST["status"];
	$status = str_replace("'",'&#39;',$status);//take out single quotes so they dont mess up the sql
	if($status != ''){
		$query = "INSERT INTO streams (message) VALUES ('$status')";
		mysql_query($query) or die(mysql_error());
	}else{
		//$status was empty
	}
}else{
	//status was not set
}
?>

also like ardav said

var status=$(".input").val();

would be better being

var status=$("#content").val();

.input refers to the class which is usually assigned to many elements but #content is an id and only given to one element. val() is correct

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.