why form data not passing? - javascript

I tried to make document for upload image in php but the document getiamge.php not accepting the form data and also giving warning.
index.php
<html>
<head>
<meta charset="utf-8">
<title>Image Upload</title>
<script>
function Insert()
{
var image= document.getElementById("image").value;
var name= document.getElementById("name").value;
if (image == "" && name == "")
{
document.getElementById("message").innerHTML="Please Fill The Fields";
return;
}
else if (image == "" || name == "")
{
document.getElementById("message").innerHTML="Please Fill The Form Correctly.";
return;
}
else
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("message").innerHTML = this.responseText;
}
};
xmlhttp.open("POST","getimage.php?name="+name+"&image="+image,true);
xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
xmlhttp.send();
}
}
</script>
</head>
<body>
<form enctype="multipart/form-data">
<input type="file" id="image">
<input type="text" id="name">
<input type="button" value="Upload" onClick="Insert()">
<h6 id="message">yyys</h6>
</form>
</body>
</html>
getimage.php
<?php
include("2nd.php");
$a=$_POST['image'];
$name=$_POST['name'];
$path="images/".basename($_FILES['image']['name']);
$image=base64_encode($a);
$query="INSERT INTO images(image,name) VALUES ('".$image."','".$name."')";
$insert=mysqli_query($conn,$query);
if($insert)
{
move_uploaded_file($_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['name']))
{
echo "Uploaded Successfully";
}
else
{
echo "Not Uploaded successfully";
}
}
else
{
echo "Not Inserted";
}
?>
And the warnings are these
Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0
Notice: Undefined index: image in D:\xampp\htdocs\image\getimage.php on line 4
Notice: Undefined index: name in D:\xampp\htdocs\image\getimage.php on line 5
Notice: Undefined index: image in D:\xampp\htdocs\image\getimage.php on line 7
Notice: Undefined index: image in D:\xampp\htdocs\image\getimage.php on line 16
Notice: Undefined index: image in D:\xampp\htdocs\image\getimage.php on line 17
Not Uploaded successfully

Notice: Undefined index: image error is coming because you are sending image in POST data, so it will come in $_POST['image'] to receive in $_FILES['image']['name'] you have to use FormData() like as below:
'
function Insert()
{
var image= document.getElementById("image").files[0];
var name= document.getElementById("name").value;
if (image == "" && name == "")
{
document.getElementById("message").innerHTML="Please Fill The Fields";
return;
}
else if (image == "" || name == "")
{
document.getElementById("message").innerHTML="Please Fill The Form Correctly.";
return;
}
else
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("message").innerHTML = this.responseText;
}
};
xmlhttp.open("POST","getimage.php",true);
xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
var formData = new FormData();
formData.append("image", image);
formData.append("name", name);
xmlhttp.send(formData);
}
}
</script>'

Related

Get Smarty-formatted data from PHP script

My PHP script, that I call via XMLHttpRequest, executes a query and checks a condition, then outputs a HTML snippet with some Smarty code. When I try to insert that code in a <div> tag, either by calling the jQuery.html() function or by setting the innerHTML property, the Smarty code is printed as it is, thus not interpreted as Smarty code. How could I solve this problem?
Relevant code:
PHP Script:
<?php
$mysqli = new mysqli("localhost", "<<<SQL USERNAME>>>", "<<<SQL PASSWORD>>>", "<<<SQL DATABASE>>>");
$id = $_GET["idmf"];
$cat = $mysqli->query("<<<SQL QUERY>>>")->fetch_assoc()["category"];
if ($cat == $_GET["cat"])
echo 'blahblah';
?>
TPL file:
{foreach from=$manufacturers item=manufacturer name=manufacturers}
<div id="mffilter_{$manufacturer.id_manufacturer}"></div>
<script>
var xhttpf;
if (window.XMLHttpRequest)
{
xhttpf = new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xhttpf = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttpf.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("mffilter_{$manufacturer.id_manufacturer}").innerHTML = this.responseText;
}
};
xhttpf.open("GET", "<<<ENDPOINT>>>.php?cat=food&idmf={$manufacturer.id_manufacturer}", true);
xhttpf.send();
</script>
Try replacing your TPL file code with the following code:
{foreach from=$manufacturers item=manufacturer name=manufacturers}
<div id="mffilter_"{$manufacturer.id_manufacturer}></div>
<script>
var xhttpf;
if (window.XMLHttpRequest)
{
xhttpf = new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xhttpf = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttpf.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("mffilter_"{$manufacturer.id_manufacturer}).innerHTML = this.responseText;
}
};
xhttpf.open("GET", "<<<ENDPOINT>>>.php?cat=food&idmf="{$manufacturer.id_manufacturer}, true);
xhttpf.send();
</script>

Why does my table appear onload but not my chart?

So I have 2 functions, 1 to draw a chart, another to draw a table.
I did a
<body onload="getchart();gettable();">
but only the table gets drawn out onload!
So, I did a
<body onload="gettable();getchart();">
but oppositely, only the chart gets drawn out onload!
May I know how to draw both chart and table onload? Thank you~
My Codes down below (I did not include the server side php to mysql codes here) :
<script type="text/javascript">
function gettable(string,strings)
{
var a=string;
var b=strings;
if(a == null && b == null)
{
a = new Date().getFullYear();
b = "";
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("inputtable").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","GetTableData.php?q="+a+"&v="+b,true);
xmlhttp.send();
}
function getchart(str)
{
var b=str;
if(b == "")
{
b = new Date().getFullYear();
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("inputchart").innerHTML = xmlhttp.responseText;
var a=JSON.parse(xmlhttp.responseText);
ab=a[1];
cd=a[2];
ef=a[3];
gh=a[4];
//google.setOnLoadCallback(drawMouseoverVisualization());
drawMouseoverVisualization();
}
};
xmlhttp.open("GET","getData.php?q="+b,true);
xmlhttp.send();
}
function drawMouseoverVisualization() {
var barsVisualization;
var data = new google.visualization.DataTable();
data.addColumn('number', 'Quarter');
data.addColumn('number', 'Frequency');
data.addRows([[1,ab],[2,cd],[3,ef],[4,gh]]);
var options={
hAxis: {title: 'Quarter'},
vAxis: {title: 'Frequency'},
trendlines: {0: {type: 'exponential', //type: 'polynomial',type: 'linear'
degree:3,
color: 'red'
}}
};
barsVisualization = new google.visualization.ScatterChart(document.getElementById('chart'));
barsVisualization.draw(data, options);
};
google.load('visualization', '1.0', {'packages':['corechart']});
</script>
</head>
<body onload="getchart();gettable();">
<div id="inputchart" style="visibility: hidden"></div>
<form>
<select name="years" id="years" onchange="getchart(this.value)">
<?php
echo "<option>Select year</option>";
for ($i=2016;$i<date("Y")+10;$i++){
echo "<option value='$i'>$i</option>";
}
?>
</select>
</form>
<div id="chart"></div>
<form>
<input type="text" name="search" id="search" onkeyup="gettable(year.value,this.value)">
<select name="year" id="year" onchange="gettable(this.value,search.value)">
<?php
echo "<option value=2016>Select year</option>";
for ($i=2016;$i<date("Y")+10;$i++){
echo "<option value='$i'>$i</option>";}
?>
</select>
</form>
<div id="inputtable"></div>
</body>
You are using xmlhttp as a global variable.
Whichever function you run second overwrites the first value you assigned to it.
Avoid globals. Use local variables. Add var.
Perhaps you need a space in between the calling of the two functions.
http://www.htmlgoodies.com/beyond/javascript/article.php/3724571/Using-Multiple-JavaScript-Onload-Functions.htm
Otherwise you could create a function in your js that reads something like this:
function start () {func1(); func2();}
And just call start() when body is loaded.

show result dynamically in search box

I want a search box which shows the result just below it when I click the search button without reloading the page.
for this I've js function called ShowUsers(str)
like this
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
document.getElementById("txtHint").innerHTML =
xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
And a html form like this :
<form>
<input type="text" name="q"/>
<script type="text/javascript">var x = document.getElementById("q").value; </script>
<input type="button" value="search" onclick="showUser(x)">
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
I've one php file to show the result called as GetUser.php
I think the var x deos'nt pass to the ShowUser(str) function in the input tag I think this is not a right way to pass a js var into html event
so what can i do to show the data
Access document.getElementById("q").value directly in showUser function rather than passing x to showUser function.
function showUser() {
var str = document.getElementById("txtHint").value;
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
document.getElementById("txtHint").innerHTML =
xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}

how to execute the javascript returned by AJAX response in the webpage (response = html+javascript)

my buyresult.php page is working properly when i give give the url as buyresult.php?q=Mysore
but the in response i get after ajax call(both html and javascript is response).. the javascript is not working... how to solve it.. I'm new to web programming. Please HELP..
I've googled and got the result as to use
new Ajax.Updater(divID,URL,{asynchronous:true,evalScripts: true});
but i don't know where to put this function
my index.php file is as follows :
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","buyresult.php?q="+str,true);
xmlhttp.send();
}
}
</script></head>
<body>
<label for="city">Enter City :</label><input class="w-input" id="city" type="text" placeholder="Enter the city name (required)" name="city" data-name="city" required="required" onchange="showUser(this.value)">
<div id="txtHint"><b>Results will be displayed Here...</b></div>
</body>

My Ajax javascript code isn't working

My Ajax javascript code isn't working...
Here is the code:
function getMessages() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("box").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "process.php");
xmlhttp.send();
}
I have my php script just doing a test saying: this is a test.
I can't figure out what I did wrong. Chrome javascript debugger says that I have an unxpected token "."
PHP Code:
<?php if (!$_COOKIE["name"]) {
echo "<form method='get' action = 'process.php'><input type = 'text' name = 'name'><input type = 'submit' value = 'Submit'></form>";
} else {
echo '
function getMessages(){var xmlhttp;
if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest (); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4&&xmlhttp.status==200) { document.getElementById("box").innerHTML=xmlhttp.responseText; } }
xmlhttp.open("GET","process.php") ;
xmlhttp.send(); }
var is={ie:navigator.appName=="Microsoft InternetExplorer",java:navigator.javaEnabled(),ns:navigator.appName=="Netscape",ua:navigator.userAgent.toLowerCase(),version:parseFloat(navigator.appVersion.substr(21))||parseFloat(navigator.appVersion),win:navigator.platform=="Win32"
}
is.mac=is.ua.indexOf("mac")>=0;
if(is.ua.indexOf("opera")>=0){is.ie=is.ns=false;is.opera=true;}
if(is.ua.indexOf("agecko")>=0)
{is.ie=is.ns=false;is.gecko=true;
}';
echo "<textarea id=box rows=20 cols=20></textarea>
<form method='get' action = 'process.php'>
<input type = 'text' name = 'message'>
<input type = 'submit' value = 'Submit'>
</form>";
} ?>
xmlhttp.readyStat should be xmlhttp.readyState perhaps?
readyStat should be readyState for one. Rest seems okay.
xmlhttp.send() to xmlhttp.send(null) can prevent IE bugs too

Categories