I have a multi-page form for customizing some sort of product. The radio buttons are dynamically generated based on the values from the database. What I want to happen is prevent the form from shifting to the next page if the required radio button groups doesn't have a selected value. I have tried JQuery validation plugin:
$('#customize-form').validate({ // initialize plugin
ignore:":not(:visible)",
rules: {
shape: { required:true },
size: { required:true },
tier: { required:true },
flavors: { required:true },
}
});
Now this part here shows the next page. Works perfectly except the fact that valid() seems to always return true and it goes to the next page.
// Next button click action
btnnext.click(function(){
if(current < widget.length){
// Check validation
if($("#customize-form").valid())
{
widget.show();
widget.not(':eq('+(current++)+')').hide();
}
}
hideButtons(current); //hides buttons which are not needed such as the submit button if it's not the end of the form
})
Here is how I generate the radio buttons dynamically:
<div id=step1 class='options step'>
<section class=section-title>Shape</section>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT * FROM product_shape_t");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
echo '<ul class="radios">';
echo '<li>';
echo '<input type="radio" name="shape" id="'.$row['ShapeName'].'">';
echo '<label for="'.$row['ShapeName'].'">'.$row['ShapeName'].'</label>';
echo '</li>';
echo '</ul>';
}
?>
</div>
Here is the full html:
<html>
<head>
</head>
<body>
<form method=POST id=customize-form action ="">
<div id=step1 class='options step'>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT MAX(CustomizedProductID) as 'Max ID' FROM customized_products_t");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$custom_id = $row['Max ID'] + 1;
echo "<label class=custom_id style='display:none'>".$custom_id."</label>";
}
?>
<section class=section-title>Shape</section>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT * FROM product_shape_t");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
echo '<ul class="radios">';
echo '<li>';
echo '<input type="radio" name="shape" id="'.$row['ShapeName'].'">';
echo '<label for="'.$row['ShapeName'].'">'.$row['ShapeName'].'</label>';
echo '</li>';
echo '</ul>';
}
?>
<section class=section-title>Size</section>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT * FROM product_size_t");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
echo '<ul class="radios">';
echo '<li>';
echo '<input type="radio" name="size" id="'.$row['SizeName'].'">';
echo '<label for="'.$row['SizeName'].'">'.$row['SizeName'].'</label><br />';
echo '<label for="'.$row['SizeDesc'].'">'.$row['SizeDesc'].'</label>';
echo '</li>';
echo '</ul>';
}
?>
<section class=section-title>Tier</section>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT * FROM product_tier_t");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
echo '<ul class="radios">';
echo '<li>';
echo '<input type="radio" name="tier" id="'.$row['TierName'].'">';
echo '<label for="'.$row['TierName'].'">'.$row['TierName'].'</label><br />';
echo '</li>';
echo '</ul>';
}
?>
</div>
<div id=step2 class='options step'>
<section class=section-title>Flavor</section>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT * FROM product_flavor_t");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
echo '<ul class="radios">';
echo '<li>';
echo '<input type="checkbox" name="flavors" id="'.$row['FlavorName'].'">';
echo '<label for="'.$row['FlavorName'].'">'.$row['FlavorName'].'</label><br />';
echo '</li>';
echo '</ul>';
}
?>
<section class=section-title>Sides (Optional)</section>
<?php
include('connect_db.php');
$query = $dbc->prepare("SELECT g.IngredientID, g.IngredientsName FROM ingredients_t g JOIN inventory_t i ON g.IngredientID = i.IngredientsID JOIN ingredient_type_t t ON t.IngTypeID = i.IngredientTypeID WHERE t.IngTypeName='sides'");
$query->execute();
$result = $query->get_result();
$total = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
echo '<ul class="radios">';
echo '<li>';
echo '<input type="checkbox" name="ingredients" id="'.$row['IngredientsName'].'">';
echo '<label for="'.$row['IngredientsName'].'">'.$row['IngredientsName'].'</label><br />';
echo '</li>';
echo '</ul>';
}
?>
<section class=section-title>Template (Optional)</section>
<?php include('get_templates.php');?>
</div>
<div id=step3 class='options step'>
<section class=section-title>Image (Optional)</section>
<div class=image-display><img src="" width="100" style="display:none;" /></div>
<?php
echo '<form method=post enctype=multipart/form-data>
<section style="margin-bottom:10px">
<label class="paragraph-font2 full-width">Attach an image:</label>
</section>
<section style="margin-bottom:15px">
<input class="paragraph-font2 full-width" type=file name=uploaded_img id=uploaded_img accept="image/*">
</form>';
?>
</div>
<div id=step4 class='options step'>
<section class=section-title>Writings (Optional)</section>
<?php
echo '<form method=post>
<section class=add-notes>
<textarea id=writings class="lightfields-base lighttxtarea-regular" maxlength="250" placeholder="Do we have to write something on the cake?"></textarea>
</section>
</form>';
?>
<div class=separator-div></div>
<section class=section-title>Additional notes (Optional)</section>
<?php
echo '<form method=post>
<section class=add-notes>
<textarea id=additional class="lightfields-base lighttxtarea-medium" maxlength="999"></textarea>
</section>
</form>';
?>
</div>
<div id=step5 class='options step'>
<section class=section-title>Check your cake</section>
<div id=final_image class=final-image-display><img src="" width="100" style="display:none;" /></div>
<div class=details>
<label><span class=desc>Shape: </span><span id=shape></span></label>
<label><span class=desc>Size: </span><span id=size></span></label>
<label><span class=desc>Flavor: </span><span id=flavor></span></label>
<label><span class=desc>Sides/Mix-ins: </span><span id=sides></span></label>
<label><span class=desc>Writings: </span><span id=write></span></label>
<label><span class=desc>Additional notes: </span><span id=add_notes></span></label>
</div>
</div>
<div class=button-separator>
<label>
<button class='btn btn-info action next'>Next <span class='glyphicon glyphicon-arrow-right'></span></button>
</label>
<label>
<button class='btn btn-info action submit'>Add to cart <span class='glyphicon glyphicon-arrow-right'></span></button>
</label>
<label>
<button class='btn btn-info action back'><span class='glyphicon glyphicon-arrow-left'></span> Back</button>
</label>
</div>
</form>
</body>
</html>
<script>
$(document).ready(function(){
var current = 1;
widget = $(".step");
btnnext = $(".next");
btnback = $(".back");
btnsubmit = $(".submit");
// Init buttons and UI
widget.not(':eq(0)').hide();
hideButtons(current);
$('#customize-form').validate({ // initialize plugin
ignore:":not(:visible)",
rules: {
shape: { required:true },
size: { required:true },
tier: { required:true },
flavors: { required:true },
}
});
// Next button click action
btnnext.click(function(){
if(current < widget.length){
// Check validation
if($("#customize-form").valid())
{
widget.show();
widget.not(':eq('+(current++)+')').hide();
$('#step'+(current-1)).removeClass('active');
$('#step'+(current-1)).addClass('complete');
$('#step'+current).removeClass('disabled');
$('#step'+current).addClass('active');
//This part gets the values chosen to be displayed on page 5
/*if(current == 5)
{
var shape = $('input[type=radio][name=shape]:checked').attr('id');
var size = $('input[type=radio][name=size]:checked').attr('id');
var tier = $('input[type=radio][name=tier]:checked').attr('id');
var flavors = [];
$("input[name=flavors]:checked").each(function()
{
flavors.push($(this).attr('id'));
});
var sides = [];
$("input[name=ingredients]:checked").each(function()
{
sides.push($(this).attr('id'));
});
var writings = $(this).find('#writings').val();
var additional = $(this).find('#additional').val();
document.getElementById("shape").innerHTML=shape;
document.getElementById("size").innerHTML=size;
var flav;
for (var i=0; i<flavors.length; i++) {
flav += flavors[i];
}
document.getElementById("flavor").innerHTML=flavors;
document.getElementById("sides").innerHTML=sides;
document.getElementById("write").innerHTML=writings;
document.getElementById("add_notes").innerHTML=additional;
}*/
}
}
hideButtons(current);
})
// Back button click action
btnback.click(function(){
if(current > 1){
current = current - 2;
if(current < widget.length){
widget.show();
widget.not(':eq('+(current++)+')').hide();
//This part adjusts the steps bar
/*$('#step'+current).removeClass('complete');
$('#step'+current).addClass('active');
$('#step'+(current+1)).removeClass('active');
$('#step'+(current+1)).addClass('disabled');*/
}
}
hideButtons(current);
})
btnsubmit.click(function(){
if($("#customize-form").valid())
{
var id = $(this).find('.custom_id').text();
var quantity = 1;
window.location.href = "add_custom_to_cart.php?id=" + id + "&quantity=" + quantity;
return false;
}
})
$('#uploaded_img').change( function(event) {
$(".image-display img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
var image_name = $(this).attr('src',URL.createObjectURL(event.target.files[0]));
document.getElementById('final_image').src = image_name;
});
$('.select-template').on('click', function(e){
var temp_id = $('.temp_id').text();
$.ajax({
type: 'POST',
url: 'add_selected_temp.php',
data: {
template: temp_id
}
});
});
$().maxlength();
});
// Hide buttons according to the current step
hideButtons = function(current){
var limit = parseInt(widget.length);
$(".action").hide();
if(current < limit) btnnext.show();
if(current > 1) btnback.show();
if (current == limit) {
btnnext.hide();
btnsubmit.show();
}
}
</script>
Am I doing something wrong or should I use an alternative solution? Thank you.
I have solved the problem by making a custom validation. There seems to be a problem with my markup that's why JQuery validation plugin won't apply. Here is my custom validation.
btnnext.click(function(){
if(current < widget.length){
// Check validation
var empty = true;
if(current == 1)
{
if($("input[name='shape']:checked").val() &&
$("input[name='tier']:checked").val() &&
$("input[name='tier']:checked").val())
{
empty = false;
}
}
if(current == 2)
{
if($("input[name='flavors']:checked").val())
{
empty = false;
}
}
if(current == 3)
{
if(document.getElementById("uploaded_img").files.length != 0)
{
empty = false;
}
}
if(current == 4)
{
if($("#writings").val())
{
empty = false;
}
}
if(current == 5)
{
empty = false;
}
if(!empty)
{
widget.show();
widget.not(':eq('+(current++)+')').hide();
//only gets values to be display on page 5 please ignore
/*if(current == 5)
{
var shape = $('input[type=radio][name=shape]:checked').attr('id');
var size = $('input[type=radio][name=size]:checked').attr('id');
var tier = $('input[type=radio][name=tier]:checked').attr('id');
var flavors = [];
$("input[name=flavors]:checked").each(function()
{
flavors.push($(this).attr('id'));
});
var sides = [];
$("input[name=ingredients]:checked").each(function()
{
sides.push($(this).attr('id'));
});
var writings = $("#writings").val()
var additional =$("#additional").val();
document.getElementById("shape").innerHTML=shape;
document.getElementById("size").innerHTML=size;
var flav;
for (var i=0; i<flavors.length; i++) {
flav += flavors[i];
}
document.getElementById("flavor").innerHTML=flavors;
document.getElementById("sides").innerHTML=sides;
document.getElementById("write").innerHTML=writings;
document.getElementById("add_notes").innerHTML=additional;
}*/
}
}
hideButtons(current);
})
I want to give credit to OffirPe'er for all the help and heads up about possible markup conflict.
Related
function some_name() {
if (document.getElementById('first').checked)
document.getElementById('second').checked=true;
else
document.getElementById('second').checked=false;
}
<?php
$idno = $vt->veriTemizle($_GET['idno']);
$rest = $vt->sqlSorgu("Select * From rest ");
foreach($vt->fetch_assocAll() as $rest){
echo '
<input type="checkbox" name="option1[]" onclick="some_name();" id="first" value="'.$rest["price"].'" />
<input type="hidden" name="extra[]" onclick="some_name();" id="second" value="'.$rest["productname"].'-'.$rest["price"].' " />
';
}
?>
is a code I have been trying to do for a long time.
I have to select another one when the checkboxes listed above are clicked.
The first record listed is working, others are not
You can generate IDs for all checkboxes like and solve the problem like this
<script>
function some_name( i) {
console.log(i);
if (document.getElementById('first'+i).checked)
document.getElementById('second'+i).checked=true;
else
document.getElementById('second'+i).checked=false;
}
</script>
<?php
$row['price'] = 100;
$row['productname'] = "ABC";
$arr[] = $row;
$row['price'] = 1001;
$row['productname'] = "asd";
$arr[] = $row;
$row['price'] = 1002;
$row['productname'] = "ABC2";
$arr[] = $row;
$row['price'] = 1003;
$row['productname'] = "ABC1";
$arr[] = $row;
$i=0;
foreach($arr as $rest){
echo '
<input type="checkbox" name="option1[]" onclick="some_name('.$i.');" id="first'.$i.'" value="'.$rest["price"].'" />
<input type="checkbox" name="extra[]" onclick="some_name('.$i.');" id="second'.$i.'" value="'.$rest["productname"].'-'.$rest["price"].' " />
';
$i++;
}
?>
My Code is:
<form method="POST" action="" style="margin-top: 30px;">
<div class="form-group" style="display: inline-block;">
<label for="Name" class="label-control" >Device Price</label>
<input type="text" name="price" class="form-control">
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3">
<?php
$r = 0;
$sql = "SELECT * FROM Subscribes ";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
if ($r == 5) {
echo "</div><div class='col-md-3'>";
$r = 0;
}
$r++;
$p = $row['price'];
echo "<label style='font-size: 16px; margin: 20px;'>";
echo "<input name='checkbox[]' type='checkbox' ";
echo "value=" . $p . " >" . $row['sub_name'];
if ($p == 0) {
echo "<label style='color:#f00'> Free </label>";
}
echo "</label>";
}
?>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group" style="margin-left: 35%;">
<button class="btn btn-info" name="sum" style="font-size: 24px;">TOTAL</button>
<label style="margin-left: 6%; font-size: 24px;">
<?php
if (isset($_POST['sum'])) {
$price = $_POST['price'];
$price = $price + $price * .52;
$total = $price;
if (!empty($_POST['checkbox'])) {
foreach ($_POST['checkbox'] as $value) {
$total = $total + $value;
}
}
echo $total;
}
?></label>
</div>
</div>
</div>
You can put a checked property to a checkbox:<input type="checkbox" checked>"
In your case, you have to check the POST data in the first block aswell and add the checked property to checkboxes that have been submitted.
echo "<input name='checkbox[]' type='checkbox' ";
echo "value=" . $p;
//if value is in the array of submitted checkboxes, add the checked property
if(array_search($p ,$_POST["checkbox"]) >= 0) echo "checked";
echo ">" . $row['sub_name'];
You can almost just check posted value to see if checked[$r] is on, but in the case where the user leaves some of the boxes unchecked, those don't get posted.
So, you need a way to identify each checkbox. You could do this by naming them like this when you echo them:
$id = $row['id']; // or whatever your id is
echo "<input name='checkbox[$id][]' type='checkbox' value=$p >"
Now, when the user submits the form, you know exactly which checkboxes should be checked. So you can simply add the checked attributed when you render them:
echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>"
So altogether:
<?php
$r = 0;
$sql = "SELECT * FROM Subscribes ";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
if ($r == 5) {
echo "</div><div class='col-md-3'>";
$r = 0;
}
$r++;
$p = $row['price'];
$id = $row['id'];
$checked = null;
if(array_key_exists('checkbox', $_POST) && array_key_exists($id, $_POST['checkbox']){
$checked = "checked";
}
echo "<label style='font-size: 16px; margin: 20px;'>";
echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>" . $row['sub_name'];
if ($p == 0) {
echo "<label style='color:#f00'> Free </label>";
}
echo "</label>";
}
?>
Here is an image of what I'm trying to accomplish -
Example of my Database Table -
My goal is to get a similar product to the product that is currently displaying without refreshing the page. I am trying to find similar products is by using check boxes.
I first got the id using $_GET['id'] which should be equal to one of the values in my table.
I then used PDO Fetch to get the product name, brand, quanity and price of that particular id and store it as a string.
What I need help with is using JQuery/AJAX to get the checkboxes that are checked and then send the information to a PHP file that would check if filter results match with any data from the table.
How can I do this?
This is my product.php file
<?php
require ('includes/db.php');
$id = $_GET['id']; //Getting the ID in URL. ex products.php?id=12
$stmt = $handler->prepare("SELECT * FROM products WHERE id = '$id' ");
$result = $stmt->execute(array());
$products = $stmt->fetch(PDO::FETCH_ASSOC);
$prod_name = $products['prod_name']; //Product Name
$brand = $products['brand']; //Product Brand
$quantity = $products['quantity']; //Product Quantity
$calories = $products['calories']; //Product Calories
$price = $products['price']; //Product Price
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo "$brand $prod_name"; ?></title>
</head>
<body>
<h1><?php echo $prod_name; ?></h1>
<br />
<p>Brand = <?php echo " $brand"; ?></p>
<p>Quantity = <?php echo " $quantity"; ?></p>
<p>Calories = <?php echo " $calories"; ?></p>
<p>Price = <?php echo " $price"; ?></p>
<br />
<p style="text-align: center;">Find Similar Products</p>
<form>
<div class="checkboxes">
<label>
<input name="brand" type="checkbox" value="<?php echo $brand; ?>">
<span>Brand</span> <!--Brand Checkbox-->
</label>
</div>
<div class="checkboxes">
<label>
<input name="quanitity" type="checkbox" value="<?php echo $quantity; ?>">
<span>Quantity</span> <!--Quantity Checkbox-->
</label>
</div>
<div class="checkboxes">
<label>
<input name="calories" type="checkbox" value="<?php echo $calories; ?>">
<span>Calories</span> <!--Calories Checkbox-->
</label>
</div>
<div class="checkboxes">
<label>
<input name="price" type="checkbox" value="<?php echo $price; ?>">
<span>Price</span> <!--Price Checkbox-->
</label>
</div>
</form>
</body>
</html>
I managed to solve this out, glad I didn't give up.
My product.php File
<?php
require ('/db.php');
$id = $_GET['id']; //Getting the ID in URL. ex products.php?id=12
$stmt = $handler->prepare("SELECT * FROM products WHERE id = '$id' ");
$result = $stmt->execute(array());
$products = $stmt->fetch(PDO::FETCH_ASSOC);
$prod_name = $products['prod_name']; //Product Name
$brand = $products['brand']; //Product Brand
$quantity = $products['quantity']; //Product Quantity
$calories = $products['calories']; //Product Calories
$price = $products['price']; //Product Price
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo "$brand $prod_name"; ?></title>
</head>
<body>
<h1><?php echo $prod_name; ?></h1>
<br />
<p>Brand = <?php echo " $brand"; ?></p>
<p>Quantity = <?php echo " $quantity"; ?></p>
<p>Calories = <?php echo " $calories"; ?></p>
<p>Price = <?php echo " $price"; ?></p>
<br />
<p style="text-align: center;">Find Similar Products</p>
<form method="post" action="">
<div class="checkboxes">
<label>
<input name="brand" type="checkbox" value="<?php echo $brand; ?>">
<span>Brand</span> <!--Brand Checkbox-->
</label>
</div>
<div class="checkboxes">
<label>
<input name="quanitity" type="checkbox" value="<?php echo $quantity; ?>">
<span>Quantity</span> <!--Quantity Checkbox-->
</label>
</div>
<div class="checkboxes">
<label>
<input name="calories" type="checkbox" value="<?php echo $calories; ?>">
<span>Calories</span> <!--Calories Checkbox-->
</label>
</div>
<div class="checkboxes">
<label>
<input name="price" type="checkbox" value="<?php echo $price; ?>">
<span>Price</span> <!--Price Checkbox-->
</label>
</div>
</form>
<div class="filter_container">
<div style="clear:both;"></div>
</div>
<script type="text/javascript" src="/js/filter.js"></script>
<input name="prod_name" value="<?php echo $prod_name ?>" style="display:none;"/>
<!--Hidden product name-->
</body>
</html>
Here is my JS File
$(document).ready(function() {
function showValues() {
var brand;
var quantity;
var calories;
var price;
//Gets product name
var prod_name = $('input[name="prod_name"]').val();
//Gets brand
if($('input[name="brand"]').is(':checked'))
{brand = $('input[name="brand"]').val();} else {brand = ""}
//Gets quantity
if($('input[name="quantity"]').is(':checked'))
{quantity = $('input[name="quantity"]').val();} else {quantity = ""}
//Gets calories
if($('input[name="calories"]').is(':checked'))
{calories = $('input[name="calories"]').val();} else {calories = ""}
//Gets price
if($('input[name="price"]').is(':checked'))
{price = $('input[name="price"]').val();} else {price = ""}
$.ajax({
type: "POST",
url: "/query.php",
data: {'brand':brand, 'quantity':quantity, 'calories':calories, 'prod_name':prod_name},
cache: false,
success: function(data){
$('.filter_container').html(data)
}
});
}
//Call function when checkbox is clicked
$("input[type='checkbox']").on( "click", showValues );
//Remove checked when checkbox is checked
$(".checkboxes").click(function(){
$(this).removeAttr('checked');
showValues();
});
});
Here is my PHP File
<?php
include('/db.php');
$prod_name = $_POST['prod_name'];
$Cbrand = $_POST['brand'];
$Cquantity = $_POST['quantity'];
$Ccalories = $_POST['calories'];
$Cprice = $_POST['price'];
if(!empty($Cbrand))
{
$data1 = "brand = '$Cbrand' AND";
}else{
$data1 = "";
}
if(!empty($Cquantity))
{
$data2 = "quantity = '$Cquantity' AND";
}else{
$data2 = "";
}
if(!empty($Ccalories))
{
$data3 = "calories = '$Ccalories' AND";
}else{
$data3 = "";
}
if(!empty($Cprice))
{
$data4 = "price = '$Cprice'";
}else{
$data4 = "";
}
$main_string = "WHERE $data1 $data2 $data3 $data4"; //All details
$stringAnd = "AND"; //And
$main_string = trim($main_string); //Remove whitespaces from the beginning and end of the main string
$endAnd = substr($main_string, -3); //Gets the AND at the end
if($stringAnd == $endAnd)
{
$main_string = substr($main_string, 0, -3);
}else if($main_string == "WHERE"){
$main_string = "";
}
else{
$main_string = "WHERE $data1 $data2 $data3 $data4";
}
if($main_string == ""){ //Doesn't show all the products
}else{
$sql = "SELECT COUNT(*) FROM products $main_string";
if ($res = $handler->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
$sql = "SELECT * FROM products $main_string";
foreach ($handler->query($sql) as $pro) {
if(($pro['prod_name'] == $prod_name) && ($res->fetchColumn() < 2))
{
//The product currently being displayed is blank when using the filter
}
else{
?>
<!------------------------------------------------------------------------------------------------------------------------------------------------->
<div class="form-result">
<td><?=strtoupper($pro['brand']) + " " + strtoupper($pro['prod_name']); ?></td>
</div>
<!------------------------------------------------------------------------------------------------------------------------------------------------->
<?php
}
}
} /* No rows matched -- do something else */
else {
?>
<div align="center"><h2 style="font-family:'Arial Black', Gadget, sans-serif;font-size:30px;color:#0099FF;">No Results with this filter</h2></div>
<?php
}
}
}
$handler = null;
$res = null;
?>
I'm currently trying to use comments on my website. The comments will be used on posts I have.
The thing is, I'm currently using an iframe to show them.
I would like the code to be on the same page as my posts.
I'll show you what I mean by posting my code:
comment_frame.php:
<?php
if (isset($_POST['postComment' . $getid . ''])) {
$post_body = $_POST['post_body'];
$posted_to = "Chris";
$insertPost = mysql_query("INSERT INTO post_comments VALUES ('','$post_body','$user','$posted_to','0','$getid')");
echo "<p style='color: green'>Comment Posted!</p><br /><br />";
}
// Get relevent comments
$get_comments = mysql_query("SELECT * FROM post_comments WHERE post_id='$getid' ORDER BY id DESC");
$count = mysql_num_rows($get_comments);
if ($count != 0) {
while ($comment = mysql_fetch_assoc($get_comments)) {
$comment_body = $comment['post_body'];
$posted_to = $comment['posted_to'];
$posted_by = $comment['posted_by'];
$removed = $comment['post_removed'];
echo "<b><a href='$posted_by' target='_blank'>$posted_by</a> said: <br /></b>".$comment_body."<hr /><br />";
}
}
else
{
// Do nothing!
}
?>
home.php:
<?php
// If the user is logged in
$getposts = mysql_query("SELECT * FROM posts WHERE user_posted_to='$user' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$id = $row['id'];
$body = $row['body'];
$date_added = $row['date_added'];
$added_by = $row['added_by'];
$user_posted_to = $row['user_posted_to'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./img/default_pic.png";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
?>
<script language="javascript">
function toggle<?php echo $id; ?>() {
var ele = document.getElementById("toggleComment<?php echo $id; ?>");
var text = document.getElementById("displayText<?php echo $id; ?>");
if (ele.style.display == "block") {
ele.style.display = "none";
}
else
{
ele.style.display = "block";
}
}
</script>
<?php
echo "
<br />
<div class='newsFeedPost'>
<div class='newsFeedPostOptions'>
<a href='javascript:;' onClick='javascript:toggle$id()'>Show Comments</a>
<div style='float: left;'>
<a href='$added_by'><img src='$profilepic_info' height='60' /></a>
</div>
<div class='posted_by'><a href='$added_by'>$added_by</a> wrote:</div>
<br /><br />
<div style='max-width: 600px; height: auto;'>
$body<br /><br /><br /><p />
</div>
Like – Re-Shout! – Comment
<br /><iframe src='./comment_frame.php?id=$id' frameborder='0' style='max-height: 200px; width: 100%; min-height: 10px;'></iframe>
</div>
</div>
";
}
}
?>
As you can see it shows the comment_frame.php page by using an iframe. I would like all of that code to be in a single page. How would I do that?
put ur home.php code in else condition if user adding new comment the first condition will work otherwise second one
if (isset($_POST['postComment' . $getid . ''])) {
your code here
}else {
// home.php code herer
}
I have this text field
<?php if($row_questionset['Constructor']=="TextField"){?>
<?php while ($row_Answer=mysql_fetch_array($AnswersValue)){ ?>
<fieldset class="field" >
<?php $question=$row_questionset['QuestionIDFKPK'];?>
<span class="symbol">
<?php echo $row_questionset['QuestionValue']; ?>
<input class="validate[required] text-input" type="text" name="sampletextanswer<?= $row_Answer['QuestionIDFK'];?>[]" id=" < ?php echo $question ?> " value="<?= $answerSelect=$row_Answer['AnswerIDPK'] ; ?>" onblur="textfieldfocus()" <?php if ($row_Answer['UserAnswer']!='') {echo "checked=checked";} ?> >
</span>
</fieldset>
<?php } ?>
<?php } ?>
And i tried with these scripts for save in a table usage the info that user introduce on texfield each time that he click on it but don't works.What can i do ?
<script>
function textfieldfocus(){
var node_list = document.getElementsByTagName('input');
for (var i = 0; i < node_list.length; i++) {
var node = node_list[i];
if (node.getAttribute('type') == 'text'){
UpdateItem('<?php echo $US_ID ?>;' + this.id + ';' + this.value);
}
}
};
</script>