javascript won't run inside php - javascript

I have the following php page,
which has a dropdown box and when the value closed is chosen it is meant to open a text box.
I have embdedded the javascript inside php but for some reason it is not working.
<?php
require_once "/var/www/users/user.php";
$user = new CHUser(2);
require_once "/var/www/template/template.php";
$template = new CHTemplate();
require_once "/var/www/ENGINEER/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();
$todays_date = date('Y-m-d H:i:s');
$ID = $_GET['id'];
//$ID = $_POST['ID'];
$ChurchNo = $_POST['ChurchNo'];
$SerialNo = $_POST['SerialNo'];
$MachineType= $_POST['MachineType'];
$Model = $_POST['Model'];
$Location= $_POST['Location'];
$Priority = $_POST['Priority'];
$Issue = $_POST['Issue'];
$Status = $_POST['Status'];
$Call_Opened = $_POST['Call_Opened'];
//echo "test component $Component";
$wip = $BundleProgress-> ListTicketById($_GET['id']);
foreach($wip as $x) {
$ChurchNo = $x['ChurchNo'];
$SerialNo = $x['SerialNo'];
$MachineType= $x['MachineType'];
$Model = $x['Model'];
$Location= $x['Location'];
$Priority = $x['Priority'];
$Issue = $x['Issue'];
$Status = $x['Status'];
$Call_Opened = $x['Call_Opened'];
$Operator = $x['Operator'];
$Engineer = $x['Engineer'];
$Notes = $x['Notes'];
}
$content .="
<html>
<head>
<title></title>
<script language='JavaScript' type='text/javascript'>
function ShowTB(obj,id){
txt=obj.options[obj.selectedIndex].text;
if (txt.match('Pending')){
document.getElementById(id).style.visibility='hidden';
}
if (txt.match('Closed')){
document.getElementById(id).style.visibility='visible';
}
}
</script>
</head>
<body>";
$content .="
<p><b>You are changing the status of call no: $ID </b></p>
<table>
<tr><td> <b>ChurchNo</b>:$ChurchNo </td> </tr>
<tr><td> <b>SerialNo</b>: $SerialNo</td> </tr>
<tr><td> <b>MachineType</b>: $MachineType</td> </tr>
<tr><td> <b>Model</b>: $Model </td> </tr>
<tr><td> <b>Location</b>:$Location </td> </tr>
<tr><td> <b>Priority</b>: $Priority </td> </tr>
<tr><td> <b>Issue</b>: $Issue </td> </tr>
<tr><td> <b>Status</b>: $Status </td> </tr>
<tr><td> <b>Date Opened</b>:$Call_Opened </td> </tr>
<tr><td> <b>Operator</b>:$Operator </td> </tr>
<tr><td> <b>Logged By</b>:$Engineer </td> </tr>
</table>
</br>
</br>
<table>
<form method='post' action='UpdateTicket2.php'>
<tr><td><b>Assign to: </b></td> <td> <select name='Assignedto'>
<option value='David'>David</option>
<option value='Ashley'>Ashley</option>
<option value='Pete'>Pete</option>
<option value='Mark'>Mark</option>
<option value='Rory'>Rory</option>
</select>
</td>
</tr>
";
$content .="
<tr><td><b>Status </b></td>
<td>
<select onchange=\"ShowTB(this,'Date');\" >
<option >Open</option>
<option >Closed</option>
<option >Pending</option>
</select>
<br>
<input id='Date' style='visibility:hidden;' >
</td>
<tr> ";
$content .="
<tr><td><b>Notes:</b></td><td><textarea name='Notes' cols='25' rows='5'>
$Notes
</textarea></td></tr>
<tr><td><b>Current Time: $todays_date</b></td></tr>
<tr><td><input type='hidden' name='ID' id='ID' value='$ID' /></td></tr>
<tr><td><input type='submit' name ='add'/></td></tr>
</form>
</table>
";
$content .="
</br>
<p> Warning.... </p>
<form action='delete.php' method='post'>
<input type='submit' value='Delete Record' />
<p><input type='hidden' name='ID' id='ID' value='$ID' /></p>
</form>";
$template->SetTag("content", $content);
echo $template->Display();
?>

For testing purposes:
comment all until your program begin in
/*
*/
$content .="
then change:
$template->SetTag("content", $content);
echo $template->Display();
for:
echo $content;
finally, inside the script tags write:
alert('Javascript is working');
I made it and got the program running. You problem is in the $template class, not in this code.
Cheers,

Related

How to calculate the rest of the inputs

I have a table and I am calculating the difference of hours but only first row of the table calculates me. and I have a method where created a new table is if the date is different
$(document).ready(function() {
getHrs();
});
function getHrs()
{
hstart=$('#horaInicio').val();
hend=$('#horaTermino').val();
hr1 = (hstart).split(":");
hr2 = (hend).split(":");
hrstart=(hr1[0]);
minstart=(hr1[1]);
hrend=(hr2[0]);
minend=(hr2[1]);
totalstart=parseInt((hrstart*60)) + parseInt(minstart);
totalend=parseInt(hrend*60) + parseInt(minend);
rsthr=(totalstart - totalend);
total=(rsthr / 60).toFixed(2);
$('#resultado').val(total);
}
here my table, I create a table for each different day.
<?php if ($seguimientos): ?>
<?php $fechas = array();
foreach ($seguimientos as $seguimiento) {
$fechas[$seguimiento-> fecha][] = $seguimiento;
}
?>
<?php foreach ($fechas as $fecha): ?>
<table class="table table-hover">
<thead>
<tr>
<th>Fecha</th>
<th>Orden</th>
</tr>
</thead>
<?php foreach ($fecha as $seguimiento): ?>
<tr>
<td>
<?php echo $seguimiento-> fecha; ?>
</td>
<td>
<?php echo $seguimiento-> horaInicio; ?>
<input type="hidden" id="horaInicio" value=" echo $seguimiento-> horaInicio;">
</td>
<td>
<?php echo $seguimiento-> horaFin; ?>
<input type="hidden" id="horaInicio" value=" echo $seguimiento-> horaFin;">
</td>
<td>
<input type="text" id="resultado" value="" readonly>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
<?php endif; ?>
could be the problem the ID must be unique
Yes, that is the problem, try not to use id's but rather data attributes, or classes.
like this: <input type="hidden" data-start="horaInicio" value=" echo $seguimiento-> horaInicio;">
Then your selector becomes: hstart=$('[data-start]').val(); And the same for the other input.

unable to submit form data using ajax?

i am very new to php and have no idea of javascript, have done all js work using tutorials.Also tried to use existing solutions from stackoverflow, but no success !
What i am trying to do is trying to update database values using 'submit' button on the basis of values called from 'select option'.
Problem is that no data is getting updated in DB after i click on submit button (on viewtest.php), I have also tried to add the 'update.php' to form action.
Here's the screenshot of viewtest.php
here's my code:
viewtest.php
<form method="POST" action="">
<table border="1px"><tr> <td>
<select required name="tstname" id="test" onChange="fetch_ques()">
<option> Select Test Name</option>
<?php
$res=mysqli_query($connection,"select * from test_detail");
while($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["test_name"]; ?>">
<?php echo
$row["test_name"]; echo' - - ['. $row['test_category'].']'; ?> </option>
<?php
}
?>
</select>
</div>
<td>
<div id="qnos"><select></select></div></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" name="btnSubmit" id="vform" value="Update Question"> </td></tr>
</form>
</table>
<div id="ques"></div>
<script type="text/javascript">
function fetch_ques()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","getq.php?
tstnm="+document.getElementById("test").value,false); // function id
xmlhttp.send(null);
document.getElementById("qnos").innerHTML=xmlhttp.responseText; // div id
}
function display_ques()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","displayq.php?
qnos="+document.getElementById("quesdd").value,false);
xmlhttp.send(null);
document.getElementById("ques").innerHTML=xmlhttp.responseText;
}
</script>
<script type="text/javascript">
$(document).ready(function()
{
$("#vform").submit(); (function()
// document.getElementById("vform").submit();(function()
{
location.href='update.php?
qno='+$("#quesno").val()+'&qn='+$("#ques").val()+'&c1='+$("#a1").val()+'&c2='+$
("#a2").val()+'&c3'+$("#a3").val()+'&c4='+$("#a4").val()+'&cr='+$("#cr").val();
});
});
</script>
<?php
if(isset($_GET['st']) && $_GET['st'] !== "")
{
echo"updated";
}
else echo "Error: ".mysqli_errno();
?>
displayq.php //Used to fetch data in select menu
<?php
include '../connect.php';
$quesno=$_GET["qnos"];
if($quesno!="")
{
$qry=mysqli_query($connection,"select * from quesadd where quesid='$quesno'");
echo "<table name='ques'>";
while($ftc=mysqli_fetch_array($qry))
{
?>
<form method="POST" action="">
<table name="ques">
<tr><td align="center" colspan="2"> <!-- // Comment <input type="submit" name="submit" id="upq" value="Update Question">--></td> </tr>
<tr> <td align="center"> <b> Question : <input type="text" id="quesno" value="<?php echo $ftc["quesid"];?>" disabled></b></td>
<td> <textarea name="ques" id="ques" cols="100" rows="3" placeholder="Please Input The Question Here !"> <?php echo $ftc['ques'];?></textarea> </td> </tr>
<tr> <td width="25%" align="center" colspan="2"> <br> <b>Choices</b> </td> </tr>
<tr> <td align="center" colspan="2"><b>1.</b> <input type="text" id="a1" name="a1" value="<?php echo $ftc['c1'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b>2.</b> <input type="text" id="a2" size="20px" name="a2" value="<?php echo $ftc['c2'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b>3.</b> <input type="text" id="a3" size="20px" name="a3" value="<?php echo $ftc['c3'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b>4.</b> <input type="text" id="a4" size="20px" name="a4" value="<?php echo $ftc['c4'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b><font color="maroon">Correct Answer</font></b> <input type="text" size="20px" id="cr" name="correct" value="<?php echo $ftc['answer'];?>"> </td> </tr>
</table>
</form>
<?php
}
echo "</table>";
}
?>
</tr> </td>
</table>
</form>
update.php //Used to update question by getting values from 'viewtest.php'
<?php
include '../connect.php';
$qn=$_GET['qno'];
$qname=$GET['qn'];
$a1=$GET['c1'];
$a2=$GET['c2'];
$a3=$GET['c3'];
$a4=$GET['c4'];
$acr=$GET['cr'];
$update=mysqli_query($connection,"update quesadd SET ques='$qname',
c1='$a1',c2='$a2',c3='$a3',c4='$a4',answer='$acr' where quesid='$qn' ");
if($update==true)
{
header('location:viewtest.php?st=true');
}
?>
I can see multiple problems, don't know if all the problems will go away but it will help.
$("#vform").submit(); (function()
The line above says if a <form> with the id="vform" has been submitted, execute this function. Your form does not have an id so it will never trigger. One other problem is that the function is not part of the trigger. It should be something like
$("#vform").submit(function()
The complete code could look like:
<script type="text/javascript">
$(document).ready(function(){
$("#vform").submit(function(e){
e.preventDefault();
location.href='update.php?qno='+$("#quesno").val()+'&qn='+$("#ques").val()+'&c1='+$("#a1").val()+'&c2='+$("#a2").val()+'&c3'+$("#a3").val()+'&c4='+$("#a4").val()+'&cr='+$("#cr").val();
return false;
});
});
</script>
Use AJAX to submit your form and refresh/redirect the page with javascript.
viewtest.php
<?php
include '../connect.php';
?>
<form method="POST" action="">
<table border="1px">
<tr>
<td>
<select required name="tstname" onchange="display_ques()" id="test">
<option> Select Test Name</option>
<?php
$res = mysqli_query($connection, "select * from test_detail");
while ($row = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row["test_id"]; ?>">
<?php echo
$row["test_name"];
echo ' - - [' . $row['test_category'] . ']'; ?> </option>
<?php
}
?>
</select>
</td>
<td>
<div id="qnos">
<select>
<!-- put code here for showing question id/number -->
</select></div>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnSubmit" id="vform" value="Update Question"></td>
</tr>
</table>
</form>
<div id="ques"></div>
<h1 id="status"></h1>
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
function display_ques() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("ques").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "displayq.php?qnos=" + document.getElementById("test").value, true);
xmlhttp.send(null);
}
function fetch_ques() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "getq.php?tstnm = " + document.getElementById("test").value, false); // function id
xmlhttp.send(null);
document.getElementById("qnos").innerHTML = xmlhttp.responseText; // div id
}
$(document).ready(function () {
$('#vform').on('click', function (e) {
e.preventDefault();
var data = $('form#update').serialize();
$.post('update.php', data, function (response) {
// response is the data returned from php echo json_encode(['results' => true/false]), from update.php
var res = JSON.parse(response);
if (res.results == true) {
$('#status').html('Data was updated');
} else {
$('#status').html('Error occured while trying to update data.');
console.log(response);
}
});
});
});
</script>
I have given your form and id to be able to serialize it using jQuery and I have also added a hidden input to hold the id of the current question.
displayq.php
<?php
include '../connect.php';
$quesno = $_GET["qnos"];
if($quesno != "") : ?>
<form id="update" method="POST" action=""> <!-- added an id to the form -->
<table name="ques">
<?php
$qry = mysqli_query($connection,"select * from quesadd where quesid='$quesno'");
while($ftc = mysqli_fetch_array($qry)) : ?>
<tr><td align="center" colspan="2"> <!-- // Comment <input type="submit" name="submit" id="upq" value="Update Question">--></td> </tr>
<tr> <td align="center"> <b> Question : <input type="text" id="quesno" value="<?php echo $ftc["quesid"];?>" disabled></b></td>
<td> <textarea name="ques" id="ques" cols="100" rows="3" placeholder="Please Input The Question Here !"> <?php echo $ftc['ques'];?></textarea> </td> </tr>
<!-- added this hidden input to hold the question number -->
<input type="text" id="quesno" name="quesno" value="<?php echo $ftc["quesid"];?>" hidden>
<tr> <td width="25%" align="center" colspan="2"> <br> <b>Choices</b> </td> </tr>
<tr> <td align="center" colspan="2"><b>1.</b> <input type="text" id="a1" name="a1" value="<?php echo $ftc['c1'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b>2.</b> <input type="text" id="a2" size="20px" name="a2" value="<?php echo $ftc['c2'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b>3.</b> <input type="text" id="a3" size="20px" name="a3" value="<?php echo $ftc['c3'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b>4.</b> <input type="text" id="a4" size="20px" name="a4" value="<?php echo $ftc['c4'];?>"> </td> </tr>
<tr> <td align="center" colspan="2"><b><font color="maroon">Correct Answer</font></b> <input type="text" size="20px" id="cr" name="correct" value="<?php echo $ftc['answer'];?>"> </td> </tr>
<?php endwhile; ?>
</table>
</form>
<?php endif; ?>
Then in your update.php you can access the values using $_POST
<?php
include '../connect.php';
$qn = trim($_POST['quesno']);
$qname = trim($_POST['ques']);
$a1 = trim($_POST['a1']);
$a2 = trim($_POST['a2']);
$a3 = trim($_POST['a3']);
$a4 = trim($_POST['a4']);
$acr = trim($_POST['correct']);
$sql = "update quesadd SET ques='$qname', c1='$a1',c2='$a2',c3='$a3',c4='$a4',answer='$acr' where quesid='$qn'";
if (mysqli_query($connection, $sql)) {
// returning data to jQuery, as response
// jQuery will parse this as json {results: true}
echo json_encode(['results' => true]);
} else {
// returning data to jQuery, as response
// jQuery will parse this as json {results: false}
echo json_encode(['results' => false]);
}

Live validation adding/updating data in a table php,ajax,javascript

Can help me to solve this?
I got a table and I have added 2 buttons to add or update information.
I wanna make a validation in the "description" field where **you can only write letters and less than 255 characters, and in the "name" field you can only write less than 50 characters. Anyway, I can not click on the button "save" or "add" respectively (the button is locked until everything is right).
Also, the page must show "In real time (before clicking the button) the errors in each field".
Here is my code, when adding new informationin the table:
<?php include('connect.php');
$error="";
if(isset($_POST['btnsave']))
{
$career_id=$_POST['carrera'];
$name=$_POST['txtname'];
$description=$_POST['txtdescription'];
$hourss=$_POST['txthours'];
if($_POST['txtid']=="0")
{
$a_sql=mysql_query("INSERT INTO subjects VALUES('','$career_id','$name','$description','$hourss')");
if($a_sql)
{
header("location:index.php");//
}
}else{
echo "Update";
}
}
$sqlm = mysql_query("SELECT * FROM careers");
$options = "";
while($result = mysql_fetch_array($sqlm)){
$options .= "<option value='".$result['id']."'>".$result['name']."</option>";
}
?>
<h2 align="center">ADD NEW SUBJECT</h2>
<form method="Post">
<table align="center">
<tr>
<td>Career:</td>
<td><select name='carrera'><?php echo $options; ?></select><input type="hidden" name="txtid" value="0" /></td>
</tr>
<tr>
<td>Name:</td>
<td><input type='text' name='txtname'/></td>
</tr>
<tr>
<td>Description:</td>
<td><input type='text' name='txtdesription'/></td>
</tr>
<tr>
<td>Hours:</td>
<td>
<select name='txthours'/>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="2">2 (two)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="4">4 (our)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="6">6 (six)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="8">8 (eight)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="10">10 (ten)</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value=save name='btnsave'/></td>
</tr>
</table>
</form>
And the another code updates the information in the table:
<?php include('connect.php');
$error="";
if(isset($_GET['edit']))
{
$ident=$_GET['iden'];
$row=mysql_query("SELECT * FROM subjects WHERE id=$ident");
$st_row=mysql_fetch_array($row);
}
?>
<h2 align="center">UPDATE SUBJECT</h2>
<form method="Post" action=''>
<table align="center">
<tr>
<td>Career:</td>
<td><input type='text' name='txtcarreras_id' value="<?PHP echo $st_row['career'] ?>"/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type='text' name='txtnombre' value="<?PHP echo $st_row['name'] ?>"/></td>
</tr>
<tr>
<td>Description:</td>
<td><input type='text' name='txtdescripcion' value="<?PHP echo $st_row['description'] ?>"/></td>
</tr>
<tr>
<td>Hours:</td>
<td><input type='text' name='txtcarga_horaria' value="<?PHP echo $st_row['hours'] ?>"/></td>
</tr>
<tr>
<td></td>
<td><input type='submit' value="save" name='btnsave'/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['btnsave']))
{
$career_id=$_POST['txtcarreras_id'];
$name=$_POST['txtnombre'];
$description=$_POST['txtdescripcion'];
$hours=$_POST['txtcarga_horaria'];
$a_sql=mysql_query("UPDATE materias SET career='$career_id', name='$name', description='$description', hours='$hours' WHERE id='$ident'");
if($a_sql)
{
header("location:index.php");
}
}
?>
I know, there are a lot of ways to solve this but i wanna the very best method!
Hope you can help me :)
Thanks!
You can do that using Javascript. With Jquery or another library it's even easier.
Using jQuery:
$('input[name=txtname]').keyup(function() {
if ($(this).val().length < 50) {
// Do Error stuff here
} else {
// Remove error stuff here
}
});
If you have jQuery and use the above keyup event handler, it should display as you type.

Javascript : Uncaught TypeError

<script>
function more(){
var MoreDetails = document.getElementById('MoreDetails');
MoreDetails.style.display ="block";
}
function same(){
var SameProduct = document.getElementById('SameProduct');
SameProduct.style.display = "block";
}
</script>
<!DOCTYPE HTML>
<?php
$con = mysqli_connect('localhost', 'root', '', 'test') or die("Cannot connect to datbase");
// $seller = $_SESSION['sellMail'];
?>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
<html>
<head>
</head>
<body>
<div id = "ProductForm">
<form id="product" method="POST" enctype = "multipart/form-data" novalidate>
<table align="center" width="750" border ="2">
<tr align="center">
<td colspan="7"><h2>Insert Product</h2></td>
</tr>
<tr>
<td align="right"><b>Type of Product: </b></td>
<td>
<input list="text" name="type" placeholder = "Select a type" required>
<datalist id="text">
<?php
$run = mysqli_query($con, "Select * from typeproduct");
while($row = mysqli_fetch_array($run)){
$type_title = $row['type_title'];
echo "<option>$type_title</option>";
}
?>
</datalist>
</td>
</tr>
<tr>
<td align="right"><b>Product Category: </b></td>
<td>
<select name = "prd_cat" required>
<option>Select a Category</option>
<?php
$get_cat = "Select * from categories";
$run_cat = mysqli_query($con, $get_cat);
while($row = mysqli_fetch_array($run_cat)){
$cat_id = $row['cat_id'];
$cat_name = $row['cat_name'];
echo "<option>$cat_name</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Brand: </b></td>
<td>
<select name = "prd_brand" required>
<option>Select a Brand</option>
<?php
$get_brn = "Select * from brands";
$run_brn = mysqli_query($con, $get_brn);
while($row = mysqli_fetch_array($run_brn)){
$brn_id = $row['brand_id'];
$brn_name = $row['brand_title'];
echo "<option>$brn_name</option>";
}
?>
</select>
</td>
</tr>
<tr align="center">
<td><input type="submit" name="prdDetails" value="Insert Product"></td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_POST['prdDetails'])){
$brand = $_POST['prd_brand'];
$type = $_POST['type'];
$cat = $_POST['prd_cat'];
$select = mysqli_query($con, "Select * from products where type = '$type' AND cat='$cat' AND brand = '$brand'");
if(mysqli_num_rows($select)==0){
echo "<script>more();</script>";
}else{
echo "<script>same();</script>";
$i=1;
?>
<table id="SameProduct" style="display:none">
<tr>
<td>S.No</td>
<td>Product Image</td>
<td>Product Title</td>
<td>Product Description</td>
<td>Same Product</td>
</tr>
<?php
while($row = mysqli_fetch_array($select)){
$img = $row['img'];
$title = $row['title'];
$desc = $row['p_desc'];
?>
<tr>
<td><?php echo $i;?></td>
<td><img src="Product_image/<?php echo $img;?>"</td>
<td><?php echo $title;?></td>
<td><?php echo $desc;?></td>
<td><button>Got the Same Product</button></td>
</tr>
<?php
$i++;
}
?>
<tr>
<td><button>Product Not in list</button></td>
</tr>
</table>
<?php
}
?>
<?php
}
?>
<form method="POST" id="MoreDetails" enctype="multipart/form-data">
<table style="display:none">
<tr>
<td align="right"><b>Product Image: </b></td>
<td><input type="file" name="prd_img" required/></td>
</tr>
<tr>
<td align="right"><b>Product Price: </b></td>
<td><input type="text" name="prd_price" required/></td>
</tr>
<tr>
<td align="right"><b>Product Description: </b></td>
<td><textarea name="prd_desc" cols = "20" rows="10" required></textarea></td>
</tr>
<tr>
<td align="right"><b>Product Keywords: </b></td>
<td><input type="text" name="prd_keyword" required/></td>
</tr>
<tr align="center">
<td colspan="7"><input type="submit" name="insertPrd" id="insertPrd" value="Insert Now"/></td>
</tr>
</table></form>
</body></html>
<?php
if(isset($_POST['insertPrd'])){
$title = $_POST['prd_title'];
$categ = $_POST['prd_cat'];
$brand = $_POST['prd_brand'];
$price = $_POST['prd_price'];
$desc = $_POST['prd_desc'];
$keywords = $_POST['prd_keyword'];
$img = $_FILES['prd_img']['name'];
$img_temp = $_FILES['prd_img']['tmp_name'];
move_uploaded_file($img_temp, "Product_image/$img");
$insert_prd = "INSERT INTO products(cat, brand, title, price, p_desc, img, keyword, seller) VALUES ('$categ', '$brand', '$title', '$price', '$desc', '$img', '$keywords', '$seller')";
$insert = mysqli_query($con, $insert_prd) or die("Cannot Insert");
if($insert){
echo "<script>alert('Product has been added');</script>";
echo "<script>window.open('Seller.php','_self')</script>";
}else{
echo "<script>alert('Sorry! The Product cannot be added');</script>";
echo "<script>window.open('Seller.php','_self')</script>";
}
}
?>
Whenever I am clicking on the prdDetails button, an error is there that
"Uncaught TypeError: Cannot read property style of 'null'" whether more() is called or same() is called.
I am currently working on the XAMPP server.
Other than that everything is working fine.
Your calls to more() and same() occur before the elements which they reference in the DOM.
Move the calls to more() and same() to somewhere after the elements MoreDetails and SameProduct.
This is because it document.getElementById('MoreDetails'); does not receive an DOM element and variable MoreDetails is null.
You should call function more() after DOM elemenet with id MoreDetails, but you call it before so you can't get this DOM element.

EDIT: add an identifier to a dynamically generated row

I'm pulling down two tables from the db. There's a Javascript function that adds a row from Table2 into the Table1. What i've been trying to do is get the data from Table1 on the confirm_trade.php page so I can put it into the db but can't figure out how to pull that row(item_id). I thought putting the table in a form would allow me to access them through the $_POST but that's not working. How to add an identifier to the jquery row so I can grab on the confirm page?
The appended rows coming from the jQuery function are the rows I need the item_id from.
function addto(obj)
{
var $row = $(obj).parents("tr");
var $table = $("#tradee");
var item_id = $row.find(".item-id").text();
var item_name = $row.find(".item-name").text();
var item_desc = $row.find(".item-desc").text();
var newTR = $("<tr><td>"+item_id+"</td><td>"+item_name+
"</td><td>"+item_desc+"</td></tr>");
$table.append(newTR);
}
Table2:
<div id='fixedDiv'>
<form action="confirm.php" method="post">
<table align="center" id="Table2">
<tr><td>Other Item</td></tr>
<?php while($rowi =$item->fetch_assoc())
{?>
<tr>
<td>
<?php echo $rowi['item_id']; ?>
</td>
<td>
<?php echo $rowi['item_name']; ?>
</td>
<td><?php echo $rowi['item_description'];?></td>
</tr>
<?php } ?>
<tr>
<td><input type="submit" name="submit" value="Continue"/></td>
</tr>
</table>
</form>
</div>
<br>
Table 1:
<table align="center" id="Table1">
<tr><th>item_id</th>
<th>Cat_id</th>
<th>Name</th>
<th>Description</th>
</tr>
<?php while($row =$item_results->fetch_assoc())
{?>
<tr></tr>
<tr>
<td class="item-id"> <?php echo $row['item_id']; ?> </td>
<td><?php echo $cat_id = $row['cat_id']; ?> </td>
<td class="item-name"><?php echo $item_id = $row['item_name'];?></td>
<td class="item-desc"><?php echo $item_descrip = $row['item_description'];?>
</td>
<td><input type="button" class="added" onclick="addto(this)"
value="Add" />
<td><input type="button" class="removed" onclick="remove()"
value="Remove" >
</td>
</tr>
<?php } ?>
</table>
Judging by your code, you can use <input type="hidden" name="some_hidden_data" value="field-value">. be sure that you also wrap your table with <form method="post"></form>
the hidden field will be then included as $_POST['hidden_field_name'];

Categories

Resources