It saves every thing in data but not the name of updated file. It shows undefined index in line 17,18,19,20 it does not show anything in PIC column in database & nothing is stored in subjects_pic folder.
Code is shown here:
<?php
include('conect.php');
$d = '';
$qre = mysql_query("select * from courses1");
while ($res = mysql_fetch_array($qre)) {
$d .='<option value="' . $res['cid'] . '">' . $res['cname'] . '</option>';
}
?>
<?php
include('auth.php');
include('conect.php');
$msg = '';
if (isset($_POST['save'])) {
$course = $_POST['courses1'];
$subjects = $_POST['subjects'];
$name = $_POST['name'];
$file_name = $_FILES['f1']['name'];
$file_type = $_FILES['f1']['type'];
$file_size = $_FILES['f1']['size'];
$file_tmp = $_FILES['f1']['tmp_name'];
$file_path = '../subject_pic/' . $file_name;
move_uploaded_file($file_tmp, $file_path);
mysql_query("INSERT INTO up_file (cid,subid,name,pic)
VALUES ('$course','$subjects','$name','$file_name')");
$msg = '<h3>Record Saved</h3>';
}
?>
<!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>subjects</title>
<link rel="stylesheet" type="text/css" href="headerstyle.css" />
<script type="application/javascript" src="prototype.js"></script>
<script type="text/javascript">
function abc()
{
var a = $('courses1').value;
$("k").innerHTML = '<img src="ajax-loader.gif">';
new Ajax.Request("ajax1.php", {
method: 'post',
postBody: "e=" + a,
onComplete: Update_abc
});
}
function Update_abc(req)
{
var data = req.responseText;
$("k").innerHTML = '';
$("k").innerHTML = data;
//alert(data);
}
</script>
</head>
<body>
07/25/2014
<div class="fixx"><?php include('header.php'); ?></div>
<div class="tabl">
<table width="700" border="0">
<tr>
<td>
<form method="post" action="filupload.php">
<table width="700" border="1" cellpadding="10">
<tr>
<td colspan="2"><h3> ADD Files To Subjects | EDIT </h3></td>
</tr>
<tr>
<td colspan="2"><?php echo $msg; ?></td>
</tr>
<tr>
<td width="174"><h4>Select Course </h4></td>
<td width="674"><label for="courses"></label>
<select name="courses1" id="courses1" onchange="abc()">
<option value="">Select Course</option>
<?php echo $d; ?>
</select> </td>
</tr>
<tr>
<td width="174"><h4>Select Subject </h4></td>
<td width="674"><label for="subjects"></label>
<div id="k">
<select name="subjects" id="subjects"><option value="">Select Subject</option></select>
</div> </td>
</tr>
<tr>
<td><h4>File Name</h4></td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td><h4>Upload Pic</h4></td>
<td><label for="f1"></label>
<input type="file" name="f1" id="f1" /></td>
</tr>
<tr>
<td><input type="submit" name="save" id="save" value="Submit" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
use enctype="multipart/form-data" in form tag like:
<form action="filupload.php" method="post" enctype="multipart/form-data">
To upload images or any files 'enctype' should be assigned
<form method="post" action="filupload.php" enctype="multipart/form-data">
Related
Im having issues while using the SESSION variables. Im planning to add data to a SQL each time the user hit "Submit" on the generar.php script. I'm also counting on the login data of the first pag index.html here is where im having issues, whenever i hit "Submit" it will clean the variables. I checked several
forums looking for an answer but still i dont have an answer. Any ideas?
index.html
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Creacion de receta</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<br>
<div style="text-align: center;"><img style="width: 125px; height: 125px;" alt="Rpi" src="large_logo_white.png"><br></div>
<br>
<table style="text-align: left; margin-left: auto; margin-right: auto;">
<form method="post" action="generar.php">
<tbody>
<tr>
<td>Codigo del Medico</td>
<td><input name="v_codigo"><br>
</td>
</tr>
<tr>
<td>Cedula del paciente</td>
<td><input name="v_cedula"><br>
</td>
</tr>
<tr>
<td> Enviar</td>
<td align="right"><input name="submit" value="Enviar" type="submit"></td>
</tr>
</tbody>
</form>
</table>
<br>
</body>
</html>
generar.php
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Creacion de receta</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<tbody>
<tr>
<td>Medicamento</td>
<td>
<select name="v_meds"><option>Seleccionar</option><option>Rojo</option><option>Verde</option><option>Azul</option><option>Amarillo</option><option>Blanco</option><option>Negro</option></select>
<br>
</td>
</tr>
<tr>
<td>Via</td>
<td>
<select name="v_via"><option>Seleccionar</option><option>Oral</option><option>Rectal</option><option>Nasal</option><option>Otica</option><option>Oftalmica</option><option>Vaginal</option><option>Topica</option></select>
<br>
</td>
</tr>
<tr>
<td>Frecuencia</td>
<td>
<select name="v_frec"><option>Seleccionar</option><option>6</option><option>8</option><option>12</option><option>24</option></select>
<br>
</td>
</tr>
<tr>
<td>Duracion</td>
<td>
<select name="v_dur"><option>Seleccionar</option><option>5</option><option>7</option><option>15</option><option>30</option><option>PRN10</option><option>PRN20</option><option>PRN30</option></select>
<br>
</td>
</tr>
<tr>
<td> Enviar</td>
<td align="right"><input name="submit" value="Enviar" type="submit"></td>
</tr>
<tr>
<td style="text-align: center;" colspan="2" rowspan="1">Ver historial de recetas</td>
</tr>
</tbody>
</form>
<?php
error_reporting(E_ALL);
session_start();
if (isset($_REQUEST['v_codigo']) || isset($_REQUEST['v_cedula'])) {
$codigo = $_REQUEST['v_codigo'];
$cedula = $_REQUEST['v_cedula'];
}
$_SESSION['sesion_codigo'] = $codigo;
$_SESSION['sesion_cedula'] = $cedula;
//$codigo1 = $_SESSION['sesion_codigo'];
//$cedula1 = $_SESSION['sesion_cedula'];
$tabla = 'p'.$cedula1;
print 'Codigo: ' . $codigo . '<br/>';
print 'Cedula: ' . $cedula . '<br/>';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name1 = $_POST['v_meds'];
$name2 = $_POST['v_via'];
$name3 = $_POST['v_frec'];
$name4 = $_POST['v_dur'];
if ($name1 == 'Seleccionar' || $name2 == 'Seleccionar' || $name3 == 'Seleccionar' || $name4 == 'Seleccionar') {
echo "Receta contiene errores, favor llenar correctamente el formulario.<br/>";
} else {
echo $name1 . $name2 . $name3 . $name4;
}
}
?>
</body>
</html>
EDIT 1:
I change the session_start(); to the very begining of the html file. But had same result. The first time i select the variables $codigo and $cedula they are correctl setup, when i click on submit button they disapear. The idea is that after clicking the submit button I keep the losgin data to send serveral requests in a row.
Below the images of each step. Thanks again to all who take time to answer.
Login data:
codigovar
cedulavar
First time the page is loaded
After clicking the submit button
EDIT 2: I'm also using #RamRaider code suggestions to fill the variables only if they are populated.
You should start session in top
<?php
error_reporting(E_ALL);
session_start();
?>
<!DOCTYPE html>
<html>
<head>
because in your code it will show error like header already sent
when we start session in php there should not be output to page before
session_start();
So your code should be something like this:-
<?php
session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html>
<head>
The main problem, I believe, was that none of the SELECT elements had OPTION values. The call to session_start() should also be before any html output - so typically right at the very top of the script. Also, the call tha sets the session variables could set blank values as it was - a test to ensure the values are present before setting would be a good idea.
<?php
session_start();
error_reporting(E_ALL);
/* utility function to debug session and request data */
function pre($data,$title){
echo "<h1>".strtoupper($title)."</h1><pre>".print_r($data,true)."</pre>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Creacion de receta</title>
<link rel="stylesheet" type="text/css" href="default.css">
</head>
<body>
<?php
if( isset( $_SESSION['sesion_codigo'], $_SESSION['sesion_cedula'] ) ){
echo '<h1>Debug session variables</h1>';
pre($_SESSION,'session');
pre($_POST,'post');
}
?>
<form method="post">
<table>
<tbody>
<tr>
<td>Medicamento</td>
<td>
<select name="v_meds">
<option value=''>Seleccionar
<option value='Rojo'>Rojo
<option value='Verde'>Verde
<option value='Azul'>Azul
<option value='Amarillo'>Amarillo
<option value='Blanco'>Blanco
<option value='Negro'>Negro
</select>
<br>
</td>
</tr>
<tr>
<td>Via</td>
<td>
<select name="v_via">
<option value=''>Seleccionar
<option value='Oral'>Oral
<option value='Rectal'>Rectal
<option value='Nasal'>Nasal
<option value='Otica'>Otica
<option value='Oftalmica'>Oftalmica
<option value='Vaginal'>Vaginal
<option value='Topica'>Topica
</select>
<br>
</td>
</tr>
<tr>
<td>Frecuencia</td>
<td>
<select name="v_frec">
<option value=''>Seleccionar
<option value='6'>6
<option value='8'>8
<option value='12'>12
<option value='24'>24
</select>
<br>
</td>
</tr>
<tr>
<td>Duracion</td>
<td>
<select name="v_dur">
<option value=''>Seleccionar
<option value='5'>5
<option value='7'>7
<option value='15'>15
<option value='30'>30
<option value='PRN10'>PRN10
<option value='PRN20'>PRN20
<option value='PRN30'>PRN30
</select>
<br>
</td>
</tr>
<tr>
<td>Enviar</td>
<td align="right"><input name="submit" value="Enviar" type="submit"></td>
</tr>
<tr>
<td style="text-align: center;" colspan="2" rowspan="1">
Ver historial de recetas
</td>
</tr>
</tbody>
</table>
</form>
<?php
$codigo = !empty( $_REQUEST['v_codigo'] ) ? $_REQUEST['v_codigo'] : false;
$cedula = !empty( $_REQUEST['v_cedula'] ) ? $_REQUEST['v_cedula'] : false;
if( $codigo & $cedula ){
$_SESSION['sesion_codigo'] = $codigo;
$_SESSION['sesion_cedula'] = $cedula;
$tabla = 'p'.$cedula1;
printf( "Codigo: %s<br />Cedula: %s<br />", $codigo, $cedula );
}
if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
$name1 = !empty( $_POST['v_meds'] ) ? $_POST['v_meds'] : false;
$name2 = !empty( $_POST['v_via'] ) ? $_POST['v_via'] : false;
$name3 = !empty( $_POST['v_frec'] ) ? $_POST['v_frec'] : false;
$name4 = !empty( $_POST['v_dur'] ) ? $_POST['v_dur'] : false;
if( $name1 && $name2 && $name3 && $name4 ){
$message=$name1 . $name2 . $name3 . $name4;
} else {
$message="Receta contiene errores, favor llenar correctamente el formulario.<br/>";
}
echo $message;
}
?>
</body>
</html>
Try this
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
...
</html>
I have tried to insert html table multiple row data to database but not success without error.
Please refer below for my code and kindly advice what do I missed.
Retrieve data for dropdown selection
<?php
//including the database connection file
include_once("config_db.php");
$gettminfo=mysql_query("SELECT * FROM tb_tm ORDER BY tm_abb");
?>
Html Code for the table
<!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" />
<link rel="stylesheet" type="text/css" href="css/spa_invoice.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<!--Add & delete rows from table dynamically-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(function(){
$('#addRow').click(function(){
var html = $('#row_template').html();
$('#dataTable').append(html);
$(".tablerow").each(function(index) {
$(this).html(index + 1);
});
});
$('#deleteRow').click(function(){
$('#dataTable .mychkbox:checked').parents('tr').remove();
calc_ttl();
});
$('#dataTable').on('change','.select-desc',function(){
var cur_val = $(this).val();
$(this).parents('tr').find('input[name="order_unit_prc[]"]').val(cur_val);
});
$('#dataTable').on('keyup','input[name="order_qty[]"]', function(){
var qty = +$(this).val();
var unit = +$(this).parents('tr').find('input[name="order_unit_prc[]"]').val();
$(this).parents('tr').find('input[name="order_amt[]"]').val(qty*unit);
calc_ttl();
});
});
</script>
<form action="" method="post" name="form_spa_inv">
<fieldset class="row3">
<table>
<tr>
<td><input type="button" value="Add Treatment" id="addRow" /></td>
<td><input type="button" value="Remove Treatment" id="deleteRow" /></td>
</tr>
</table>
<table id="dataTable" class="form" border="1">
<tr>
<td></td>
<td>No</td>
<td>Description</td>
<td>Qty</td>
<td>Unit Price</td>
<td>Amount</td>
</tr>
</table>
<!-- table row template here -->
<table id="row_template" style="display:none">
<tr>
<td><input type="checkbox" name="chk[]" class="mychkbox" /></td>
<td class="tablerow"></td>
<td>
<select name="order_desc[]" id="order_desc" class="select-desc">
<option value="">-- Select Treatment --</option>
<?php
while($dd=mysql_fetch_array($gettminfo)) {
?>
<option value="<?php echo $dd['tm_unit_prc']?>"><?php echo $dd['tm_abb'] ?> - <?php echo $dd['tm_desc'] ?></option>
<?php
}
?>
</select>
</td>
<td>
<input type="text" name="order_qty[]" id="order_qty" size="10">
</td>
<td>
<input type="text" name="order_unit_prc[]" id="order_unit_prc" readonly>
</td>
<td>
<input type="text" name="order_amt[]" id="order_amt" readonly>
</td>
</tr>
</table>
</fieldset>
<p style="text-align: center;"><input type="submit" name="Submit" value="Create">
</form>
</body>
</html>
Insert mysql query
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
for($i=0; $i < count($_POST['order_desc']); $i++) {
$order_desc = addslashes($_POST['order_desc'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
$order_amt = addslashes($_POST['order_amt'][$i]);
//insert data to database tb_odr_dtl
$insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
mysql_query($insert_odrdtl);
//display success message
if($insert_odrdtl) {
echo "<script>alert('Invoice created successfully!')</script>";
echo "<script>window.open('cv.php','_self')</script>";
}
}
}
?>
Change server site code to following:
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
for($i=0; $i < count($_POST['order_desc']); $i++) {
$order_desc = addslashes($_POST['order_desc'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
$order_amt = addslashes($_POST['order_amt'][$i]);
//insert data to database tb_odr_dtl
$insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
//mysql_query($insert_odrdtl); //delete this line of code
//display success message
if($insert_odrdtl) {
echo "<script>alert('Invoice created successfully!')</script>";
echo "<script>window.open('cv.php','_self')</script>";
}
}
}
?>
I have this function and when I update the data I still have the same problem .
I don't know where is the error. By the way my connection file is true
This is the message i put it :
Site name Error
This is my code
<!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=windows-1256" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Admin control</title>
</head>
<body>
</body>
<div class="head">Settings</div>
<?
/*
CREATE TABLE `article`.`setting` (
`name_site` VARCHAR( 300 ) NOT NULL ,
`url_site` VARCHAR( 300 ) NOT NULL ,
`email_site` VARCHAR( 150 ) NOT NULL ,
`desc_site` TEXT NOT NULL ,
`kay_site` TEXT NOT NULL ,
`oepn_site` MEDIUMINT( 2 ) NOT NULL ,
`text_close_site` TEXT NOT NULL
) ENGINE = InnoDB;
*/
include "../include/config.php";
if (isset($_POST['submit'])){
$ns = $_POST['name_site'];
$us = $_POST['url_site'];
$es = $_POST['email_site'];
$ds = $_POST['desc_site'];
$ks = $_POST['kay_site'];
$os = $_POST['oepn_site'];
$ms = $_POST['text_close_site'];
}
if (#$_POST['updatesetting']){
if(#$ns == ''){
echo "<div class='no'> Site name Error </div>";
echo '<meta http-equiv="refresh" content="2; url=setting.php"/>';
exit;
}
else {
$update = mysql_query("update setting set
name_site='$ns',
url_site = '$us',
email_site = '$es',
desc_site = '$ds',
kay_site = '$ks',
oepn_site = '$os',
text_close_site ='$ms'
") ;
if (#isset($update)){
echo "<div class='ok'> Update Done</div>";
echo '<meta http-equiv="refresh" content="2; url=setting.php"/>';
exit;
}
}
}
$sel1 = mysql_query("select * from setting ");
$row1 = mysql_fetch_assoc($sel1);
$name_site = $row1['name_site'];
$url_site = $row1['url_site'];
$email_site = $row1['email_site'];
$desc_site = $row1['desc_site'];
$kay_site = $row1['kay_site'];
$oepn_site = $row1['oepn_site'];
$text_close_site = $row1['text_close_site']
?>
<div class="bodypanel">
<form action="setting.php" method="post">
<table width="100%" border="0" dir="rtl">
<tr>
<td>Site Name</td>
<td><input type="text" name="name_site" value="<?=$name_site;?>" ></td>
</tr>
<tr>
<td>Site link</td>
<td><input type="text" name="url_site" value="<?=$url_site;?>"></td>
</tr>
<tr>
<td>Email siten</td>
<td><input type="text" name="email_site" value="<?=$email_site;?>"></td>
</tr>
<tr>
<td>Site description</td>
<td><textarea name="desc_site" rows="4" cols="25" > <?=$desc_site;?> </textarea></td>
</tr>
<tr>
<td>Key words</td>
<td><textarea name="kay_site" rows="4" cols="25" value="<?=$kay_site;?>"> <?=$kay_site;?> </textarea></td>
</tr>
<tr>
<td>Open site</td>
<td>
<select name="oepn_site" >
<?
if ($oepn_site== 1){
echo "<option value='1'>Open to visitor</option>
<option value='2'>Close to visitor</option>";
}
else {
echo "<option value='2'>Close to visitor</option>
<option value='1'>Open to visitor</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Close message</td>
<td><textarea name="text_close_site" rows="4" cols="25"> <?=$text_close_site;?> </textarea></td>
</tr>
<tr>
<td colspan="2" class="head"><input type="submit" name="updatesetting" value="save setting"> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
change the checking of submission to
if (isset($_POST['updatesetting']))
{
$ns = $_POST['name_site'];
.
.
.
}
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 -->
This is my code for a jquery mobile application. But the form i created with php result is not working. Form is not posting data on submit. I even tried data-ajax="flase".
<table id="stock">
<? $sql = mysqli_query($con, "SELECT * FROM products WHERE `product_id` = '1'");
while($row=mysqli_fetch_array($sql)){
$name = $row['name'];
$chamecal = $row['chamecal'];
$price = $row['selling_price'];
$bprice = $row['buying_price'];
$quantity = $row['quantity'];
$manufacturer = $row['manufacturer'];
$date = $row['date'];
$edate = $row['expire'];
$pid = $row['product_id'];
?>
<form id="stockupdate" method="post" action="medupdate.php">
<tr>
<td><?=$name;?></td>
<td><?=$chamecal;?></td>
<td><?=$manufacturer;?></td>
<td><input type="text" name="medbprice" value="<?=$bprice;?>" /></td>
<td><input type="text" name="medprice" value="<?=$price;?>" /></td>
<td><?=$date;?></td>
<td><?=$edate;?></td>
<td><input type="text" name="medstock" value="<?=$quantity;?>" /></td>
<td class="ui-screen-hidden"><input type="text" name="pid" value="<?=$pid;?>" /></td>
<td>
<input type="submit" title="Update" data-icon="gear" data-theme="f" data-inline="true" data-iconpos="notext" />
</td>
</tr>
<?
}
?>
</form>
</table>
Values Handler file medupdate.php
<?php session_start();?>
<?php include_once('include/config.php');?>
<?
$pid = $_POST['pid'];
$medbprice = $_POST['medbprice'];
$medprice = $_POST['medprice'];
$medstock = $_POST['medstock'];
$sql = mysqli_query($con, "UPDATE `products` SET `quantity` = '$medstock', `buying_price` = '$medbprice' , `selling_price` = '$medprice' WHERE `product_id` = '$pid'");
if($sql){
echo 1;
}
?>
If I was to hazard a guess, it's because you're placing
<form id="stockupdate" method="post" action="medupdate.php">
directly within a table element (which is invalid markup). Move this outside your table (before your <table>). When you create invalid markup, the browsers engine tries to correct it which can lead to issues like this.
This is what chrome does to your markup:
You cannot follow the html structure, a form inside table like,
<table>
<form>
<tr>
<td>
</td>
</tr>
</form>
</table>
Here is the working code,
Form php
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<form id="stockupdate" method="post" action="medupdate.php" data-ajax="false">
<table>
<tr>
<td><input type="text" name="medbprice" value="xxxx" /></td>
<td><input type="text" name="medprice" value="yyyyy" /></td>
<td><input type="text" name="medstock" value="zzzzzz" /></td>
<td class="ui-screen-hidden"><input type="text" name="pid" value="111111" /></td>
<td>
<input type="submit" title="Update" data-icon="gear" data-theme="f" data-inline="true" data-iconpos="notext" />
</td>
</tr>
</table>
</form>
</body>
</html>
medupdate.php
<?php
session_start();
$pid = $_POST['pid'];
$medbprice = $_POST['medbprice'];
$medprice = $_POST['medprice'];
$medstock = $_POST['medstock'];
echo $pid;
// your database operations
?>