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());

?>

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';
Member Avatar for diafol

try that:

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

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

Not working

try that:

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

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

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';

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

Member Avatar for diafol

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.

Member Avatar for diafol

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.