I'm trying to upload an image through php and store it in another folder of my project. I made sure that all the permissions are set. Previously,It worked fine but now I'm getting the message "not inserted!". My folder structure is as follows
I have 3 folders in my project(Admin,images and includes) at an equal level not as siblings
Below posted is my code.
<?php
session_start();
if(!$_SESSION['admin_username'])
{
header("location:login.php");
}
?>
<html>
<head>
<title>insert your comments here.</title>
<link rel = "stylesheet" href="admin_style.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-
ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#resizable
{
width: 150px;
height: 150px;
padding: 0.5em;
}
#resizable h3
{
text-align: center;
margin: 0;
}
</style>
<script>
$(function() {
$( "#cname").resizable();
$("#postauthor").resizable();
$("#postkeywords").resizable();
$("#postcontent").resizable();
});
</script>
</head>
<body>
<div class = "header">
<h1>WELCOME TO THE ADMIN PANEL </h1>
</div>
<div class = "main">
<h1 align="center">this is where you can write your content</h1>
<form action = "insert_posts.php" method="post" class = "cmxform"
enctype="multipart/form-data" id = "posts">
<fieldset>
<table width = "600" align = "center" border="10">
<tr>
<h1 align="center">insert your posts here </h1>
</tr>
<tr>
<!--<label for="cname">post_title (required, at least 2 characters)</label>-->
<td align="center">post_title</td>
<td><input type = "text" size="30" name ="posttitle" id = 'cname' minlength =
'2' required/></td>
</tr>
<tr>
<td align="center">post_date </td>
<td><input type="text" size="30" name="postdate" id= 'datepicker' required/>
</td>
</tr>
<tr>
<td align="center">post_author</td>
<td><input type="text" size="30" name="postauthor" id='postauthor' required/>
</td>
</tr>
<tr>
<td align="center">post_image</td>
<td><input type= "file" name="postimage" required/></td>
</tr>
<tr>
<td align="center">post_keywords</td>
<td><input type="text" size="30" name="postkeywords" id = "postkeywords"
required/></td>
</tr>
<tr>
<td align="center">post_content</td>
<td><textarea cols="20" rows="20" name = "postcontent" id = "postcontent"
required/></textarea></td>
</tr>
<tr>
<td colspan="5" align="center"><input type="submit" value="submit" name =
"submit" /></td>
</tr>
</table>
</fieldset>
</form>
<script>
$("#posts").validate();
</script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<script>
$(function())
{
$("#datepicker").validate();
}
</script>
<?php
include("../includes/connect.php");
?>
<?php
if(isset($_POST['submit']))
{
$post_title = $_POST['posttitle'];
$post_date = explode('/',$_POST['postdate']);
$new_date = $post_date[2].'-'.$post_date[0].'-'.$post_date[1];
$post_author = $_POST['postauthor'];
$post_keywords = $_POST['postkeywords'];
$post_content = $_POST['postcontent'];
$post_image = $_FILES['postimage']['name'];
$image_tmp = $_FILES['postimage']['tmp_name'];
if(move_uploaded_file($_FILES['postimage']['tmp_name'],'../images/'.$post_image))
{
echo "inserted";
}
else
{
echo "not inserted";
}
?>
</div>
<div class = "side">
<h3>this is side bar</h3>
<div class = "small_side">
<h2><a href = "insert_posts.php"><input type = "button" value = "insertposts">
</a></h2>
<h2><input type = "button" value="logout"></h2>
<h2><a href = "view_posts.php"><input type = "button" value="view posts"
height="50"></a></h2>
<h2><a href = "remove_posts.php"><input type = "button" value="remove_posts">
</a></h2>
</div>
</div>
<div class = "footer">
<h3>this is the footer</h3>
</div>
</body>
</html>
Try printing - var_dump() - the $_FILES array, to check if anything was posted or you had an error in the client side.
In your script, before the next line:
if(move_uploaded_file($_FILES['postimage']['tmp_name'],'../images/'.$post_image))
Add one line with the following code:
var_dump($_FILES);
Run the script, and you will get a printed version of what does the array contains at that point. Then, check there if the file looks like it was correctly received (check if tmp_name and name looks good).
If these parameters looks good, it must be a permissions problem (Or the disk is full). Make sure that apache has Write permissions in the /images/ folder
Related
Am was already created form like this and working perfect but on last two forms not working, it displays warning-Undefined variable: reg_no and cost. Am trying to follow algorithm as previous forms but nothing happen. My goal is to update inserted data and here is my form
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit invoice</title>
<link rel="stylesheet" href="box_style.css" />
</head>
<body>
<?php
include ("db_con.php");
if(isset($_GET['edit_invoice'])){
$edit_i_id = $_GET['edit_invoice'];
$select_invoice = "select * from invoice where i_id='$edit_i_id'";
$run_query = mysqli_query($con, $select_invoice);
while ($row_invoice=mysqli_fetch_array($run_query)){
$i_id = $row_invoice['i_id'];
$reg_no = $row_invoice['reg_no'];
$cost = $row_invoice['cost'];
}
}
?>
<div class='form'>
<form action="" method="post" enctype="multipart/form-data" >
<table width="745" align="center" border="2">
<p style="text-align: center;"><strong><span style="text-decoration: underline;">EDIT INVOICE:</span></strong></p>
<tr>
<td align="right" bgcolor="#dbe5f1"><strong>Registration Number:</strong></td>
<td><input type="text" name="reg_no" id="reg_no" size="35" class="text" placeholder="Registration Number" value="<?php echo $reg_no; ?>" required=""/></td>
</tr>
<tr>
<td align="right" bgcolor="#dbe5f1"><strong>Cost(Tshs):</strong></td>
<td><input type="text" name="cost" id="cost" size="35" class="text" placeholder="Cost" value="<?php echo $cost; ?>" required=""/></td>
</tr>
<tr>
<td colspan="6" align="center" bgcolor="#dbe5f1" ><input type="submit" name="update" class="submit-button" value="SAVE CHANGES"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Remove while loop from your php code since update is for one record based on id
The code will be as :
if(isset($_GET['edit_invoice'])){
$edit_i_id = $_GET['edit_invoice'];
$select_invoice = "select * from invoice where i_id='$edit_i_id'";
$run_query = mysqli_query($con, $select_invoice);
$row_invoice = mysqli_fetch_array($run_query);
$i_id = $row_invoice['i_id'];
$reg_no = $row_invoice['reg_no'];
$cost = $row_invoice['cost'];
}
if isset($_GET['edit_invoice']) is false, your $reg_no is not present in later script (where you want to echo it).
Put $reg_no above your isset($_GET...) check and set it null or empty string.
$reg_no = null;
if (isset($_GET['edit_invoice'])) {
// your code...
}
Edit: Do the same for $cost and $i_id ;)
PLEASE consider Tom Uddings comment with SQL injections!
After the bad communication and unclear questions from yesterday from my side, I prepared something. I was the first time in a forum like this. It is tough, but good! I realized how professional the people in this forum. I know not one forum that comes near to your quality of informations!
I am not a sweet talker, this is fact! Here is the problem:
I connect to a mysql table but I can not go to the next and next and next row, the same for the previous records.
I still dont understand it deeply. What is wrong on this code? I am really very curious to know it.
When I click on previous come "undefined objects."
When I skip next then comes only one record, and then it freeze. I guess that I am on the end of the table.
I was reading so many posts here, at least 50, but I did not get it. I just did not understand it.
nice greetings from Bangkok
+++
I dont use jquery commands. I use just json parse to fetch the data, but I get not a lot ;)
It is very basic. All what I want to do is get the previous and next rows. Always only one row.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function jsRecordNext()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
alert("no luck");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'nextarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//previous row
<?php include "accesscontrol.php" ;?>
<?php
$result = mysql_query("SELECT * FROM articles WHERE id<$current_id ORDER BY id DESC LIMIT 1");
$current_id = mysql_insert_id();
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//next row
<?php include "accesscontrol+.php" ;?> // connection
<?php
$result = mysql_query("SELECT * FROM articles WHERE id>$current_id ORDER BY id ASC LIMIT 1");
$current_id = mysql_insert_id();
$result = mysql_fetch_row($resulat);
echo json_encode($result);
?>
++++++++++++++++++++++++++++++++
this is 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" dir="ltr" lang="en-US">
<META HTTP-Equiv="FileTransfer" Content="TransferEvent:url('Javascript:alert('%s')'); Transfer">
<head>
<meta charset="UTF-8" />
<title>SoB - Administration</title>
<link rel="stylesheet" href="../wp-content/themes/Kuraya-Theme/style.css" tppabs="http://www.spiritofbushido.com/wp-content/themes/Kuraya-Theme/style.css">
<link rel="pingback" href="http://www.spiritofbushido.com/xmlrpc.php">
<script type="text/javascript" src="../wp-content/themes/Kuraya-Theme/js/scroll.js" tppabs="www.spiritofbushido.com/wp-content/themes/Kuraya-Theme/js/scroll.js"></script>
<script type="text/javascript" src="../includes/jquery.js"></script>
<script type="text/javascript" src="userinfo.0.0.1.min.js"></script>
<link rel="alternate" type="application/rss+xml" title="Kuraya » Feed" href="http://www.scoobeedo.com/feed/" />
<link rel="alternate" type="application/rss+xml" title="Kuraya » Comments Feed" href="http://www.scoobeedo.com/comments/feed/" />
<link rel="alternate" type="application/rss+xml" title="Kuraya » Join Our E-mail List Comments Feed" href="http://www.scoobeedo.com/email-signup/feed/" />
<link rel='stylesheet' id='shadowbox-css-css' href="../wp-content/uploads/shadowbox-js/src/shadowbox.css-ver=3.0.3.css" tppabs="http://www.scoobeedo.com/wp-content/uploads/shadowbox-js/src/shadowbox.css?ver=3.0.3" type='text/css' media='screen' />
<link rel='stylesheet' id='shadowbox-extras-css' href="../wp-content/plugins/shadowbox-js/css/extras.css-ver=3.0.3.10.css" tppabs="http://www.scoobeedo.com/wp-content/plugins/shadowbox-js/css/extras.css?ver=3.0.3.10" type='text/css' media='screen' />
<link rel='stylesheet' id='contact-form-7-css' href="../wp-content/plugins/contact-form-7/styles.css-ver=3.1.2.css" tppabs="http://www.scoobeedo.com/wp-content/plugins/contact-form-7/styles.css?ver=3.1.2" type='text/css' media='all' />
<script type='text/javascript' src="../../ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js-ver=3.3.2.js" tppabs="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js?ver=3.3.2"></script>
<script type='text/javascript' src="../wp-includes/js/comment-reply.js-ver=20090102.js" tppabs="http://www.scoobeedo.com/wp-includes/js/comment-reply.js?ver=20090102"></script>
<link rel='prev' title='東方古玩 蔵屋' href='http://www.scoobeedo.com/%e4%b8%ad%e6%96%87%e7%89%88/' />
<link rel='next' title='Sale 20% – 40% Off Entire Inventory Before Our Newshipment Arrived. Sales End April 30th.' href='http://www.scoobeedo.com/12131-2/' />
<link rel='canonical' href='http://www.scoobeedo.com/email-signup/' />
<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
<script id="source" language="javascript" type="text/javascript">
<!--
var js_string;
document.getElementById("recordWrite").disabled = true;
function jsRecordPrevious()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
$( '#recordnumber').html( '-' );
alert("f p");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'previousarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordCurrent()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
alert("f c");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'currentarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordNext()
{
// ... the AJAX request is successful
var updatePage = function( response ) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function( req, status, err ) {
alert("f n");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'nextarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsShowArticle()
{
js_articles = JSON.parse(json_string);
document.form_articles.newItem.value=js_articles[1];
document.form_articles.newItemNo.value=js_articles[2];
document.form_articles.newMaterial.value=js_articles[3];
document.form_articles.newAge.value=js_articles[4];
document.form_articles.newItemSize.value=js_articles[5];
document.form_articles.newPrice.value=js_articles[6];
document.form_articles.newInfo.value=js_articles[7];
document.form_articles.newInfoRed.value=js_articles[8];
document.form_articles.newArrivalDate.value=js_articles[9];
if (js_articles[10]="Y")
{
document.form_articles.newArrivalDateShown.checked=true;
}
else
{
document.form_articles.newArrivalDateAhown.checked=false;
}
document.form_articles.newMainPicLink.value=js_articles[11];
document.form_articles.newItemCondition.value=js_articles[12];
}
function jsBlankArticle()
{
document.form_articles.newItem.value="";
document.form_articles.newItemNo.value="";
document.form_articles.newMaterial.value="";
document.form_articles.newAge.value="";
document.form_articles.newItemSize.value="";
document.form_articles.newPrice.value="";
document.form_articles.newInfo.value="";
document.form_articles.newInfoRed.value="";
document.form_articles.newArrivalDate.value="";
document.form_articles.newArrivalDateShown.checked=false;
document.form_articles.newMainPicLink.value="";
document.form_articles.newItemCondition.value="";
}
function jsNewArticle()
{
jsBlankArticle();
document.getElementById('formheadline').innerHTML='Article Database - Insert New Item';
document.getElementById('newItem').focus();
document.getElementById("recordWrite").disabled = false;
}
function jsWriteArticle()
{
document.getElementById('formheadline').innerHTML='Article Database - I save the new item';
document.getElementById("recordWrite").disabled = true;
}
function jsResetForm()
{
jsBlankArticle();
document.getElementById('formheadline').innerHTML='Article Database';
document.getElementById("recordWrite").disabled = true;
jsRecordCurrent()
}
jsResetForm();
// -->
</SCRIPT>
</head>
<body class="page page-id-11505 page-template-default" onload="jsRecordCurrent()">
<div id="page-wrap">s
<?php
include('../includes/header.html');
?>
<div id="container-main">
<div id="main-content">
<div class="post" id="post-11505">
<h2 class="title-page">SoB - Administration</h2>
<title>SoB - Administration</title>
<div id="recordnumber"></div>
<div class="entry">
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" name="form_articles">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td align="right">
</td>
<td align="left">
<span id="formheadline"><h2>Article Database</h2></span>
</td>
</tr>
<tr>
<td align="right">
</td>
<td align="left">
<span style="padding-right:20px"><font color="orangered" size="+1"><tt><b>*</b></tt></font>indicates a required field</span>
</td>
</tr>
<br>
<tr>
<td align="right">
<span style="padding-right:20px">Item</span>
</td>
<td>
<input id="newItem" name="newItem" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item No.</span>
</td>
<td>
<input name="newItemNo" type="text" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Arrival Date</span>
</td>
<td>
<input name="newArrivalDate" type="date" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Show in New Arrivals</span>
</td>
<td>
<input name="newArrivalDateShown" type="checkbox" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Material</span>
</td>
<td>
<input name="newMaterial" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Condition of item</span>
</td>
<td>
<input id="newItemCondition" name="newItemCondition" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Age</span>
</td>
<td>
<input name="newAge" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item Size</span>
</td>
<td>
<input name="newItemSize" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Price</span>
</td>
<td>
<input name="newPrice" type="text" maxlength="100" size="25" />
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Info Red</span>
</td>
<td>
<input name="newInfoRed" type="text" maxlength="100" size="25" />
</td>
</tr>
<tr valign="top">
<td align="right">
<span style="padding-right:20px">Infos</span>
</td>
<td>
<textarea wrap="soft" name="newInfo" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">PicLink</span>
</td>
<td>
<input id="newMainPicLink" name="newMainPicLink" type="text" maxlength="100" size="50" />
</td>
</tr>
<br><br>
<tr>
<td align="right" colspan="2">
<hr noshade="noshade" />
<input type="button" name="recordPrevious" value=" < " onclick="jsRecordPrevious()"/>
<input type="button" name="recordNext" value=" > " onclick="jsRecordNext()"/>
<input type="button" name="recordNew" value=" New " onclick="jsBlankArticle()"/>
<input type="button" name="recordEdit" value=" Edit " onclick="jsShowArticle()"/>
<span style="padding-right:20px"></span>
<input type="reset" value="Reset Form" />
<input id="recordWrite" type="button" name="recordWrite" value=" Write " onclick="jsWriteArticle()"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
<?php
include('../includes/sidemenu.html');
?>
<br class="clearfloat" />
</div> <!-- End of main container -->
</div><!-- END Page Wrap -->
<?php
include('../includes/footer.html');
?>
</body>
</html>
Hello you lack lots of Logocal and Programming skills, please improve it.
Files() are as below ->
Your 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" dir="ltr" lang="en-US">
<META HTTP-Equiv="FileTransfer" Content="TransferEvent:url('Javascript:alert('%s')'); Transfer">
<head>
<meta charset="UTF-8"/>
<title>SoB - Administration</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style type="text/css">.recentcomments a {
display: inline !important;
padding: 0 !important;
margin: 0 !important;
}</style>
<script id="source" language="javascript" type="text/javascript">
var js_string;
document.getElementById("recordWrite").disabled = true;
function jsRecordPrevious() {
// ... the AJAX request is successful
var updatePage = function (response) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function (req, status, err) {
$('#recordnumber').html('-');
alert("f p");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'previousarticle.php',
data : { currentRecord: $("[name='recordPrevious']").attr("id")},
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordCurrent() {
// ... the AJAX request is successful
var updatePage = function (response) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function (req, status, err) {
alert("f c");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'currentarticle.php',
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsRecordNext() {
// ... the AJAX request is successful
var updatePage = function (response) {
json_string = JSON.stringify(response);
jsBlankArticle();
jsShowArticle();
};
// ... the AJAX request fail
var printError = function (req, status, err) {
alert("f n");
};
// Create an object to describe the AJAX request
var ajaxOptions = {
url: 'nextarticle.php',
data : { currentRecord: $("[name='recordNext']").attr("id")},
dataType: 'json',
success: updatePage,
error: printError
};
// Initiate the request!
$.ajax(ajaxOptions);
}
function jsShowArticle() {
js_articles = JSON.parse(json_string);
//Dude , I added it (Pratik)->
$('[name="recordNext"]').attr("id",js_articles[0]);
$('[name="recordPrevious"]').attr("id",js_articles[0]);
document.form_articles.newItem.value = js_articles[1];
document.form_articles.newItemNo.value = js_articles[2];
document.form_articles.newMaterial.value = js_articles[3];
document.form_articles.newAge.value = js_articles[4];
document.form_articles.newItemSize.value = js_articles[5];
document.form_articles.newPrice.value = js_articles[6];
document.form_articles.newInfo.value = js_articles[7];
document.form_articles.newInfoRed.value = js_articles[8];
document.form_articles.newArrivalDate.value = js_articles[9];
if (js_articles[10] = "Y") {
document.form_articles.newArrivalDateShown.checked = true;
}
else {
document.form_articles.newArrivalDateAhown.checked = false;
}
document.form_articles.newMainPicLink.value = js_articles[11];
document.form_articles.newItemCondition.value = js_articles[12];
}
function jsBlankArticle() {
document.form_articles.newItem.value = "";
document.form_articles.newItemNo.value = "";
document.form_articles.newMaterial.value = "";
document.form_articles.newAge.value = "";
document.form_articles.newItemSize.value = "";
document.form_articles.newPrice.value = "";
document.form_articles.newInfo.value = "";
document.form_articles.newInfoRed.value = "";
document.form_articles.newArrivalDate.value = "";
document.form_articles.newArrivalDateShown.checked = false;
document.form_articles.newMainPicLink.value = "";
document.form_articles.newItemCondition.value = "";
}
function jsNewArticle() {
jsBlankArticle();
document.getElementById('formheadline').innerHTML = 'Article Database - Insert New Item';
document.getElementById('newItem').focus();
document.getElementById("recordWrite").disabled = false;
}
function jsWriteArticle() {
document.getElementById('formheadline').innerHTML = 'Article Database - I save the new item';
document.getElementById("recordWrite").disabled = true;
}
function jsResetForm() {
jsBlankArticle();
document.getElementById('formheadline').innerHTML = 'Article Database';
document.getElementById("recordWrite").disabled = true;
jsRecordCurrent()
}
jsResetForm();
</script>
</head>
<body class="page page-id-11505 page-template-default" onload="jsRecordCurrent()">
<div id="page-wrap">
<div id="container-main">
<div id="main-content">
<div class="post" id="post-11505">
<h2 class="title-page">SoB - Administration</h2>
<title>SoB - Administration</title>
<div id="recordnumber"></div>
<div class="entry">
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="form_articles">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td align="right">
</td>
<td align="left">
<span id="formheadline"><h2>Article Database</h2></span>
</td>
</tr>
<tr>
<td align="right">
</td>
<td align="left">
<span style="padding-right:20px"><font color="orangered" size="+1"><tt><b>*</b></tt></font>indicates a required field</span>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item</span>
</td>
<td>
<input id="newItem" name="newItem" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item No.</span>
</td>
<td>
<input name="newItemNo" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Arrival Date</span>
</td>
<td>
<input name="newArrivalDate" type="date"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Show in New Arrivals</span>
</td>
<td>
<input name="newArrivalDateShown" type="checkbox"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Material</span>
</td>
<td>
<input name="newMaterial" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Condition of item</span>
</td>
<td>
<input id="newItemCondition" name="newItemCondition" type="text" maxlength="100"
size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Age</span>
</td>
<td>
<input name="newAge" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Item Size</span>
</td>
<td>
<input name="newItemSize" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Price</span>
</td>
<td>
<input name="newPrice" type="text" maxlength="100" size="25"/>
<font color="orangered" size="+1"><tt><b>*</b></tt></font>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">Info Red</span>
</td>
<td>
<input name="newInfoRed" type="text" maxlength="100" size="25"/>
</td>
</tr>
<tr valign="top">
<td align="right">
<span style="padding-right:20px">Infos</span>
</td>
<td>
<textarea wrap="soft" name="newInfo" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<td align="right">
<span style="padding-right:20px">PicLink</span>
</td>
<td>
<input id="newMainPicLink" name="newMainPicLink" type="text" maxlength="100"
size="50"/>
</td>
</tr>
<br><br>
<tr>
<td align="right" colspan="2">
<hr noshade="noshade"/>
<input type="button" name="recordPrevious" value=" < "
onclick="jsRecordPrevious()"/>
<input type="button" name="recordNext" value=" > " onclick="jsRecordNext()"/>
<input type="button" name="recordNew" value=" New " onclick="jsBlankArticle()"/>
<input type="button" name="recordEdit" value=" Edit " onclick="jsShowArticle()"/>
<span style="padding-right:20px"></span>
<input type="reset" value="Reset Form"/>
<input id="recordWrite" type="button" name="recordWrite" value=" Write "
onclick="jsWriteArticle()"/>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
<br class="clearfloat"/>
</div>
<!-- End of main container -->
</div>
<!-- END Page Wrap -->
</body>
</html>
This is currentarticle.php
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('testDb', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$result = mysql_query("SELECT * FROM articles ORDER BY id ASC LIMIT 1");
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
This is previousarticle.php
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('testDb', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$current_id = $_REQUEST['currentRecord'];
$result = mysql_query("SELECT * FROM articles WHERE id < ".$current_id." ORDER BY id ASC LIMIT 1");
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
This is nextarticle.php
<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('testDb', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$current_id = $_REQUEST['currentRecord'];
$result = mysql_query("SELECT * FROM articles WHERE id > ".$current_id." ORDER BY id ASC LIMIT 1");
$result = mysql_fetch_row($result);
echo json_encode($result);
?>
I made changes in all files in some extent.So please modify accordingly in your code.And do proper coding from next time.
And you add validation like If its last row , disable NEXT button , if its 1st row , disable PREV button.I solved what you asked.
Thanks!
I'm struggling now for a few days to get the value of a checkbox in my code.
Basically I have an admin-page where the customer can select and deselect images that will put online.
You can select and deselect images that will be shown on the homepage, and separate on the gallery-page. Both checked is also possible.
I have another checkbox that can be selected to remove the image from the list(image_deleted).
There is still a database entry and the images are still on file-system but later on I'll create a cleanup-job.
Here is my code:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ob_start();
require('../../lib/dbconnection.php');
require("../../lib/checklogin.php");
require("includes/upload.inc.php");
$query = 'SELECT * FROM gallery where image_deleted != 1 order by id desc';
$result=$conn->query($query);
$count=$result->num_rows;
?>
<!DOCTYPE html>
<html>
<head>
<title>Classic Nails - CMS</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="ClassicNails">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/screen.css">
<link rel="stylesheet" href="../css/libs/magnific-popup.css">
<script src="../js/libs/min/jquery-min.js" type="text/javascript"></script>
<script src="../js/min/custom-min.js" type="text/javascript"></script>
<script src="js/jquery.magnific-popup.js"></script>
<script>
$(document).ready(function() {
$('.image-link').magnificPopup({
type:'image',
gallery:{
enabled:true
}
});
});
</script>
</head>
<body>
<?php include('includes/header.inc.php'); ?>
<?php include('includes/nav.inc.php'); ?>
<div class="wrapper">
<article class="content">
<h1>Foto gallery</h1>
<?php
if (isset($uploadResult)) {
echo "<p><strong>$uploadResult</strong></p>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
<p>
<label for="image">Upload image:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<input type="file" name="images" id="imagesd" />
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload" />
</p>
</form>
<div id="maincontent">
<h2>Foto informatie</h2>
<form name="FotoInformatie" id="fotoInformatie" method="post" action="">
<table>
<tr>
<td align="center"><strong>Foto<strong></td>
<td align="center"><strong>Titel</strong></td>
<td align="center"><strong>Beschrijving</strong></td>
<td align="center"><strong>Homepage</strong></td>
</tr>
<?php
while ($rows=$result->fetch_assoc()) {
?>
<tr>
<td class="hide" align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td><img src="../img/thumbs/<?php echo $rows['filename']; ?>"></td>
<td align="center"><input name="title[]" type="text" id="title" value="<?php echo $rows['title']; ?>"></td>
<td align="center"><input name="caption[]" type="text" id="caption" value="<?php echo $rows['caption']; ?>"></td>
<td><input type="checkbox" name="checkboxHome[]" id="checkBoxHome" value="<?php echo ($rows['home'] == 1) ? 'checked="checked"' : ''; ?>"/></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center">
<input type="submit" name="submit" value="Submit">
</tr>
</table>
</form>
</div>
</article> <!-- end of content -->
</div> <!-- end of container -->
<?php include('includes/footer.inc.php'); ?>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$caption = $_POST['caption'];
if ($_POST['checkboxHome'] == "") {
$checkboxHome[] = '0';
} else {
$checkboxHome[] = '1';
}
for($i=0;$i<$count;$i++){
$result1=mysqli_query($conn, "UPDATE gallery SET title='$title[$i]', caption='$caption[$i]', home='$checkboxHome[$i]' WHERE id='$id[$i]'");
header("location:/admin/foto-admin.php");
}
}
?>
The checkbox only works on the first row in my DB. When I select another record, only the first record in my db will be updated.
Another issue is that my checkbox won't be checked so I don't know based on my screen when a image is online or not. in the database I see a 1 of a 0.
I know that sql-injection is possible and I have to prepare the statements, but that is the next step when I get this checkbox-issue working.
Hope someone can help me with my code. It's giving me a headache.
Check these
Attribute name="id[]" for id field is not given. And it should get inside
if(isset($_POST['submit'])) {
$id = $_POST['id'];
}
Incorrect spelling in getting Post value
change
$checkboxHome = $_POST['checkboxHome'];
$checkboxFotoboek= $_POST['checkboxFotoboek'];
$checkboxDelete = $_POST['image_deleted'];
to
$checkboxHome = $_POST['checkBoxHome'];
$checkboxFotoboek= $_POST['checkBoxFotoboek'];
$checkboxDelete = $_POST['checkboxDelete'];
You are trying to get wrong value.
Your check-box name is checkBoxHome and you are trying to get $_POST['checkboxHome'] instead of $_POST['checkBoxHome'] .
Try $_POST['checkBoxHome'] and print it as print_r('checkBoxHome')
Same mistake in checkBoxFotoboek check-box.
try this
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$caption = $_POST['caption'];
$checkboxHome = $_POST['checkBoxHome'];
$checkboxFotoboek= $_POST['checkBoxFotoboek'];
$checkboxDelete = $_POST['checkboxDelete'];
for($i=0;$i<$count;$i++){
$result1=mysqli_query($conn, "UPDATE gallery SET title='$title[$i]', caption='$caption[$i]', home='$checkboxHome[$i]', fotoboek='$checkboxFotoboek[$i]', image_deleted='$checkboxDelete[$i]' WHERE id='$id[$i]'");
header("location:/admin/foto-admin.php");
}
}
?>
Last night I was trying to figure out how I can how I can dynamically enable and disable span#txtCaptchaDiv on my contact form at the very bottom, above the submit button.
So I added a new field to MySQL, called captcha where I wanted to 1 to show and 0 to hide
So if I add 1 to field captcha the following code will show on my form.php
<label for="code">Write code below > <span id="txtCaptchaDiv" style="color:#F00"></span><!-- this is where the script will place the generated code -->
<input type="hidden" id="txtCaptcha" /></label><!-- this is where the script will place a copy of the code for validation: this is a hidden field -->
<input type="text" name="txtInput" id="txtInput" size="30" />
If I add 0 to field captcha the captcha area will be blank on my form.php.
Can you guy help me out please?
here is my index.php code I currently have:
<?php
require_once("/config/database.php");
$con = mysql_connect($config["db_server"],$config["db_user"],$config["db_pass"]);
?>
<!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=utf-8" />
<title>Email FORM</title>
</head>
<body>
<div style="width: 550px; text-align: center;">
<span style="filter:alpha(opacity=60); opacity:.6; padding-left: 10px;"><br />
<?php
$data = mysql_query("SELECT * FROM formrelated")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
Print " ".$info['welcomemsg'] . "";
?>
</span></div>
<form id="form1" name="form1" method="post" action="submit.php" onsubmit="return checkform(this);">
<table width="454" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="123">Name</td>
<td width="325">
<input name="name" type="text" />
</td>
</tr>
<tr>
<td height="21">Address</td>
<td><input name="adress" type="text" /></td>
</tr>
<tr>
<td height="21"> </td>
<td><input name="address2" type="text" /></td>
</tr>
<tr>
<td height="21">Email</td>
<td><input name="email" type="text" /></td>
</tr>
<tr>
<td height="21">Tel</td>
<td><input name="email" type="text" /></td>
</tr>
</table>
<!--- captcha code here--->
<center>
<table width="454" height="122" border="0" cellspacing="0" cellpadding="0" background="reCAPbg.png">
<tr>
<td height="73" colspan="2" align="center" valign="middle"><label for="code"><span id="txtCaptchaDiv" style="color:#333; font-size:18px;"></span><!-- this is where the script will place the generated code -->
<input type="hidden" id="txtCaptcha" /></label></td>
<td width="136" rowspan="2"> </td>
</tr>
<tr>
<td width="145"> type the code here:</td>
<td width="173" height="47" align="center"><input type="text" name="txtInput" id="txtInput" size="20" /></td>
</tr>
</table>
</center>
<!--- captcha code ends here--->
<input name="Submit" type="button" value="submit" />
</form>
<script type="text/javascript">
//Generates the captcha function
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';
var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("txtCaptchaDiv").innerHTML = code;
</script>
<script type="text/javascript">
function checkform(theform){
var why = "";
if(theform.txtInput.value == ""){
why += "- Security code should not be empty.\n";
}
if(theform.txtInput.value != ""){
if(ValidCaptcha(theform.txtInput.value) == false){
why += "- Security code did not match.\n";
}
}
if(why != ""){
alert(why);
return false;
}
}
// Validate the Entered input aganist the generated security code function
function ValidCaptcha(){
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('txtInput').value);
if (str1 == str2){
return true;
}else{
return false;
}
}
// Remove the spaces from the entered and generated code
function removeSpaces(string){
return string.split(' ').join('');
}
</script>
</body>
</html>
This will work for you... enjoy!
<?PHP
$query = mysql_query("SELECT captcha FROM formrelated WHERE id = '1'");
while ($row = mysql_fetch_assoc($query)) {
$captchathis = $row['captcha'];
if ($captchathis == "1") {
echo "YOUR HTML CODE HERE";
}
else {
echo "BLANK";
}
}
?>
Try it like this
<?PHP
if($mysqlResult['captcha'] === 1)
{
echo $myHtml;
}
?>
Where $mysqlResult is an array with the result from the query, $mysqlResult['captcha']is the value of the row captcha from your query and $myHtml is that HTML code you just showed on your answer.
Good luck! ;)
Reffer to
http://php.net/manual/en/
EDIT:
http://www.php.net/manual/en/language.types.array.php ( Array type on the manual )
http://www.php.net/manual/en/control-structures.if.php ( If control structure on the manual )
http://www.php.net/manual/en/ref.mysql.php ( MySQL native functions. deprecated. Preffer MySQLi )
http://www.php.net/manual/en/book.mysqli.php ( MySQLi extension )
http://www.php.net/manual/en/book.pdo.php ( PDO native php class )
Another answer to explain the basic construct of IF logic.
Suppose i have some condition i want to meet to do something; in this case, the following logic
SHOW my form with the basic inputs
IF condition 'captcha = 1' is met, SHOW input2 (captcha)
SHOW rest of the HTML
it would be like this in PHP
<?PHP
echo $myFormWithBasicInputs;
if($captcha === 1)
{
echo $input2;
}
echo $restOfHTML;
?>
In your case, $myFormWithBasicInput and $restOfHTML is already outputed as HTML. All you want to do is inject an PHP code in it to check if some condition is matched. It will be like this
<html>
<!-- MY FORM WITH BASIC INPUTS -->
<?PHP
$captcha = $mySQLresult['captchaRow'];
if($captcha === 1)
{
?>
<!-- CAPTCHA INPUT HERE -->
<?PHP
}
?>
<!-- REST OF HTML -->
</html>
be aware that this is an workaround with example code.
<?PHP
$mysql_query = "SELECT captcha FROM formrelated";
$captcha = $mySQLresult['captchaRow'];
if($captcha === 1)
{
?>
<!--- CODE---->
<table width="454" height="122" border="0" cellspacing="0" cellpadding="0" background="reCAPbg.png">
<tr>
<td height="73" colspan="2" align="center" valign="middle"><label for="code"><span id="txtCaptchaDiv" style="color:#333; font-size:18px;"></span><!-- this is where the script will place the generated code -->
<input type="hidden" id="txtCaptcha" /></label></td>
<td width="136" rowspan="2"> </td>
</tr>
<tr>
<td width="145"> type the code here:</td>
<td width="173" height="47" align="center"><input type="text" name="txtInput" id="txtInput" size="20" /></td>
</tr>
</table>
<?PHP
}
?>
<!-- REST OF HTML -->
I have a php application that fetches the requests from mysql database and displays them for further approval. The form is fetched from send_req.php and is displayed inside the div on showrequests.php. This is the code for send_req.php
<table style="border:0;border-color:transparent">
<tr style="background-color:lightblue">
<td>Product ID</td>
<td>Name</td>
<td>Quantity</td>
<td><input type="checkbox" name="selectAll" /></td>
<td>Authorized Quantity</td>
</tr>
<form method="post" action="send_req.php">
<?php
$reqNum = $_POST['rId'];
echo "<h3>Request # $reqNum</h3>";
$showReqs = mysql_query("Select * from request where request_number='".$reqNum."' and status=0");
while($resultA = mysql_fetch_array($showReqs))
{
$rBy = $resultA['requested_by'];
$rTime = $resultA['request_time'];
$rId = $resultA['id'];
$pId = $resultA['product_id'];
$getPrName = mysql_query("select name from products where id='$pId'");
$prN = mysql_fetch_array($getPrName);
$prName = $prN['name'];
$rQuantity = $resultA['requested_quantity'];
$status = $resultA['status'];
?>
<tr>
<input type="hidden" name="rId[]" value="<?php echo $rId; ?>"/>
<td style="background-color:orange"><input type="text" name="prId[]" value="<?php echo $pId; ?>" readonly="readonly" style="border:0px"/></td>
<td style="background-color:orange"><input type="text" name="prName[]" value="<?php echo $prName; ?>" readonly="readonly" style="border:0px"/></td>
<td style="background-color:orange"><input type="text" name="quantity[]" value="<?php echo $rQuantity; ?>" readonly="readonly" style="border:0px"/></td>
<td style="background-color:orange"></td>
<td><input type="text" name="pQuantity[]" /></td>
</tr>
<?php }
?>
<tr>
<td></td>
<td></td>
<td></td>
<input type="hidden" name="rNum" value="<?php echo $reqNum; ?>" />
<td></td>
<td><input type="submit" name="submitReq" value="Send" id="submit_req" style="backgroundColor:Transparent;border:0;color:blue;width:100;"/></td>
</tr>
</form>
</table>
<?php
echo "Requested By:$rBy at ".substr($rTime,11,18)." ".substr($rTime,0,10);
?>
This is the showrequests.php page
<html>
<head>
<script type="text/javascript">
function getRequest(ob)
{
var id = ob.id;
if(window.XMLHttpRequest)
{
ajaxOb = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
ajaxOb = new ActiveXObject("Microsoft.XMLHTTP");
}
ajaxOb.open("POST", "send_req.php");
ajaxOb.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajaxOb.send("rId=" + id);
ajaxOb.onreadystatechange = function()
{
if(ajaxOb.readyState == 4)
{
if(ajaxOb.status == 200)
{
document.getElementById("showTable").innerHTML = ajaxOb.responseText;
}
}
}
}
</script>
</head>
<body>
<?php
$mysql_con = mysql_connect("localhost","root","") or die("Could not connect ".mysql_error());
$mysql_db = mysql_select_db("cart",$mysql_con) or die("Unable to select db ".mysql_error());
echo "<h2 align='center'>Pending Requests</h2>";
$showReq = mysql_query("Select distinct(request_number) as rNums from request where status=0");
?>
<div style="float:left;margin-right:15px;">
<br/>
<?php
while($result = mysql_fetch_array($showReq))
{
$rNum = $result['rNums'];
?>
<input type="button" name="fetchReq" id="<?php echo $rNum; ?>" value="<?php echo "Request # $rNum"; ?>" style="margin-bottom:5px;backgroundColor:Transparent;border:0;color:blue;width:100;text-Decoration:underline" onclick="getRequest(this)"/>
<?php
echo "<br/>";
}
?>
</div>
<div id="showTable" style="float: left">
</div>
</body>
</html>
My problem now is that everything works fine in chrome and IE but the form is not submitted when i click the submit button in firefox. I am using firefox 20.0.1. Update: I have removed the html,head and body tags from send_req.php
still not working
form is not allowed inside table. Please see also
Form inside a table
Regards,
Michael
Reminder : the structure of an HTML document is :
<!-- No div before html tag -->
<!DOCTYPE html> <!-- Doctype for HTML5 ; use whatever doctype you need -->
<html>
<head>
</head>
<!-- No div before body tag -->
<body>
<!-- Divs only belongs here -->
</body>
</html>
<!-- No div after html tag -->
If you don't follow this basic structure, you're forcing the browser to interpret your invalid code (+ quirks mode when you don't provide a doctype).
Some browser guess well what you tried to do, others don't, as Firefox might.
Please use a HTML validator as W3's validator to check your syntax.