Empty data sending into MySQL, instead input value - php

I have simple php/ajax/mysql chat. But unfortunately when I am sending form into database, php send off empty string to MySQL, how can I fix it ?
Here page.php code, with script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#messages").load('ajaxLoad.php');
$("#userArea").submit(function(){
$.post('ajaxPost.php', $('#userArea').serialize(), function(data){
$("#messages").append('<div>'+data+'</div>');
});
return false;
});
});
</script>
</head>
<body>
<div class="container">
<div id="messages"></div>
<form method="post" action="ajaxPost.php" id="userArea" style="margin: 0 auto; font-size: 23px; text-align: center;">
<h1>Chat</h1>
<input type="text" name="message" />
<input type="submit" value="send!" />
</form>
</div>
<?php include_once('ajaxLoad.php'); ?>
<?php include_once('ajaxPost.php'); ?>
And ajaxPost.php:
<?php
include_once('page.php');
include('config.php');
$message = $_POST['message'];
$db->Query("INSERT INTO messages(message1) VALUES ('$message')");
echo $message;
?>
If you run: alert($('#userArea').serialize()) it will show you your message.

Get rid of these lines:
<?php include_once('ajaxLoad.php'); ?>
<?php include_once('ajaxPost.php'); ?>
These scripts are only supposed to be used from AJAX, you shouldn't execute them when just displaying the original form. When you execute them with include() there's nothing in $_POST, so you insert an empty message.
You haven't shown what's in ajaxLoad.php, but from the way you use it with $("#messages").load(), I doubt that it should be run with include.
I'm pretty sure that ajaxPost.php works correctly when you call it with $.post(), and the empty rows are coming from the include_once().
I don't think it should prevent the insert, but you also shouldn't have
include_once('page.php');
in ajaxPost.php.

Related

Unable to output the value of a key in GET HTTP method through a PHP file

I have an HTML page with a search box and a submit button.
On clicking of submit button,the value entered in search box gets submitted in GET HTTP method and I am redirected to a PHP file, where I want to output the value entered in search box.
I have written the following code, but it did not work. Please help.
My index.html file :
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<center>
<h1><b>Total Pay Company</b></h1>
<form action="abc.php" method="get">
<input type="text" name="product" placeholder="Search Products" style="width: 178px; height: 26px">
<button type="submit" style="height: 31px; width: 72px; border:none">Submit</button>
</form>
</center>
</body>
</html>
My abc.php file :
<!DOCTYPE html>
<html>
<head>
<title>Redirect</title>
</head>
<body>
<h1>you searched for<b><?php print($_GET['product'])?></b></h1>
</body>
</html>
When I run my index.html file on the server, and give input in the search bar, and click on Submit, it gives the following result :
Please help me through this.

Retrieve text from a textarea after submission

Say I have a page with a textarea which acts as an input.
Then I have a Submit button and right under everything i have the
output textarea.
Now what I want to do is when the input has been submitted and
sent into the output text area, how can I then retrieve the text from the output area.
This is the code i have:
<head>
<?php error_reporting(0);
$OutputText = $_GET['OutputText'];
?>
</head>
<body>
<form action="#" method="_GET">
<textarea name="InputText">
hi
</textarea>
<input type="submit" name="submitFirstInput">
</form>
<textarea name="OutputText">
<?php echo $_GET['InputText']; ?>
</textarea>
<hr>
<p>Output String Length: <?php echo strlen($OutputText); ?> </p>
</body>
for reasons I dont understand, it cant define the $OutputText,
Do they both have to be in a form? As i have understood form's is only to send data, and testing it didn't help much either.
Keep in mind this is just a barebones version of the original, essentially i have some Input text and then through some logic it gets modified, therefor i want some statistics for the output result. So just getting the first input isnt rather useful..
adding some javascript you can sync the two textarea:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(window).load(function(){
$("#one, #two").on("change keyup", function(){
$("textarea").not($(this)).val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText" id="one"></textarea>
<textarea name="OutputText" id="two"></textarea>
<input type="submit" name="submitFirstInput">
</form>
<hr>
<?php echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length:
<?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>
Textarea must be inside the form tag, and the method must be GET (or POST)
try this:
<!DOCTYPE html>
<html lang="">
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="GET">
<textarea name="InputText">hi</textarea>
<input type="submit" name="submitFirstInput">
<textarea name="OutputText"><?php echo $_GET['InputText']; ?></textarea>
</form>
<hr>
<?php //echo '<pre>'; var_dump($_GET); echo '</pre>'; ?>
<p>Output String Length: <?php echo strlen($_GET['OutputText']); ?> </p>
</body>
</html>

Submitting jscolor result to shell_exec command

To start with: My web coding skills are nearly to non existent, I just did a bit of HTML ages ago...
And my first steps with php are just made today.
My goal is: Having a color picker and sending the picked value as an argument to an python script.
I managed to use the jscolor picker libary (http://jscolor.com/examples/) and I've been also able to run the python scripts with an (color) argument from php.
My problem is now:
How do I submit the color string (hex-str) to the exec command (LED_color)?
<?php
$LED_color="FFFFFF";
$LED_color=escapeshellarg($LED_color);
if (isset($_POST['button']))
{
$LED_color = $_REQUEST['hex-str'];
echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<html>
<head>
<title>Basic usage</title>
</head>
<body>
<div style="position:absolute; left:280px; top:10px;">
toString = <span id="hex-str"></span><br />
</div>
<script src="jscolor.js"></script>
Color: <input class="jscolor {closable:true,closeText:'Close me!',onFineChange:'update(this)'}" value="000000">
<script>
function update(picker) {
document.getElementById('hex-str').innerHTML = picker.toString();
}
</script>
<form method="post">
<p>
<button name="button">Submit color</button>
</p>
</form>
</body>
</html>
Your color picker input needs a name attribute to match your PHP code, and also needs to be inside the <form> element.
<?php
if (!empty($_POST["hex-str"])) {
$LED_color = escapeshellarg($_POST["hex-str"]);
echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Basic usage</title>
<script src="jscolor.js"></script>
</head>
<body>
<form method="post">
<p>
Color:
<input type="text" name="hex-str" value="000000" class="jscolor" data-jscolor="{closable:true,closeText:'Close me!',onFineChange:'update(this)'}"/>
toString = <span id="hex-str-display"></span>
</p>
<p>
<button type="submit">Submit color</button>
</p>
</form>
<script>
function update(picker) {
document.getElementById('hex-str-display').innerHTML = picker.toString();
}
</script>
</body>
</html>

<!DOCTYPE HTML> declaration causes div contents to NOT display

I have an html page which contains a div that displays the html from an external php file.
It works great until I add a DOCTYPE declaration to the html page. The page continues to function, except the external content does not appear in the div.
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
<head>
<title>Test Page</title>
<!--meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="./inc/jquery.js"></script>
<script type="text/javascript">
function getinfo()
{
$.post('prodinfo.php', { prodcode: prodcodeform.prodcodevar.value},
function(output)
{
$('#prodinfo').html(output).show;
});
}
function hideinfo()
{
$('#prodload').hide();
$('#openprodinfo').show();
}
function showinfo()
{
$('#prodload').show();
$('#openprodinfo').hide();
}
</script>
</head>
<body>
<input style="position:relative;" type="button" class="button" id="openprodinfo" title="Open" value="INFO" onclick="showinfo();">
<DIV id="prodload" style="position:absolute;margin-left:auto;margin-right:auto;display:none;text-align:center;background-color:#000000;z-index:200;border:1px solid #4e443b;">
<div id="prodinfo" style="position:relative;display:block;top:0;width:1000px;height:820px;background-color:#ffffff;margin-left:auto;margin-right:auto;">
</div>
<form name="prodcodeform">
<input type="text" readonly="readonly" id="prodcodevar" name="prodcodevar" value="nil" >
</form>
<div ID="prodinfobutton" style="position:relative;">
<input style="position:relative;" type="button" class="button" id="closeprodinfo" title="Close" value="CLOSE" onclick="document.getElementById('prodcodevar').value='nil'; hideinfo(); ">
</div>
<input type="button" id="button001" value="ONE" onclick="document.getElementById('prodcodevar').value='item1'; getinfo();">
<input type="button" id="button002" value="TWO" onclick="document.getElementById('prodcodevar').value='item2'; getinfo();">
</DIV>
</body>
</html>
You are switching to Standards mode, so your browser is no longer playing the game of being compatible with Internet Explorer 4.
prodcodeform.prodcodevar.value will error because prodcodeform is undefined.
You don't get a global variable for every element with an id or name in a document.
Change:
<form name="prodcodeform">
To
<form id="prodcodeform" method="post" action="prodinfo.php">
… and make it do something sane when a non-Ajax request gets posted (move it so it is around the buttons, make them submit buttons, and cancel the default event if the JS succeeds).
Then add:
var prodcodeform = document.getElementById('prodcodeform');
before you try to use the variable.
You started your body with </body> instead of <body>.

Undefined index with session

I'm developing a chatbox script, and I have this page that checks if session is set, and if so, the certain elements of code should be hidden with jQuery. Here are my pages:
input.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" />
<style type="text/css">
#import "stil.css";
</style>
<title>Untitled Document</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="postme.js"></script>
<?php
include_once('check.php');
?>
</head>
<body>
<div id="wrap">
<div id="chat">
<div id="main">
</div>
<div id="input">
<form name="form"action="test.php" method="post">
<input type="text" name="tekst" id="msg" size="72" />
<input type="submit" name="dugme" value="posalji" id="dugme" />
</form>
</div>
</div>
</div>
<div id="black">
</div>
<div id="name">
<form name="yname">
<input type="text" name="tekst2" />
<input type="button" name="dugme2" value="Enter" onclick='send()' />
</form>
</div>
</body>
</html>
sesion.php:
<?php
session_start();
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
?>
check.php:
<?php
include('sesion.php');
if (!isset($sesion_n)){
echo "<script type='text/javascript'>$('#black').hide();$('#name').hide();</script>";
}
?>
postme.js:
function send(){
$.post('sesion.php',{ime:yname.tekst2.value},function(val){
if(val!=null) {
$('#black').fadeOut();
$('#name').hide();
alert(val);
}
}
)};
So the problem is that I get this error every time I run the page:
Notice: Undefined index: ime in C:\wamp\www\AJAX\sesion.php on line 3.
So can someone tell me what I'm doing wrong here?
if(isset($_POST['ime']))
{
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
}
it seems that $_POST['ime']; is undefined and that means that you are not posting it i guess.
Are you sure that yname.tekst2.value is the correct way to access the value of the field?
If you have firebug you can check in the "console" tab what parametrs have been posted.
It appears you're loading check.php manually. That'd be a GET request, and will trash your stored value, as _POST won't be set on those pages. Probably won't be the cause of the undefined index problem, but something to consider.
Check that the session's ID value stays constant between requests. If it's different each time, you're getting a brand new blank session on each request.

Categories