How to restrict user to Upload only 3 images? - php

I'm creating a web app in Codeigniter. Here's the code in VIEW's.
<div class="bold light-gray">Add a photo or two!</div>
<p class="short">Or three, or more! Prospective Buyers love photos that highlight the features of your parts or gear.</p>
<form class="ajaxform" method="post" enctype="multipart/form-data" action='<?php echo site_url('UploadImage/upload_Image');?>'>
<input type="hidden" name="do" value="upload"/>
Upload your image <input type="file" name="Filedata" id="photo" onChange="abc()" />
<input type="hidden" name="timestamp" value="<?php echo $timestamp;?>" />
<input type="hidden" name="token" value="<?php echo md5('unique_salt' . $timestamp);?>" />
<input type="hidden" name="customer_id" value="<?php echo $customer['id'];?>" />
</form>
</div>
<!-- /.box.center -->
<?php echo form_open('/secure/add_item/'.$id, array('id'=>'listitem') ); ?>
<div id="img_cant"> <?php
if(isset($images) && !empty($images)) {
foreach($images as $key=>$img){
?>
<input type="hidden" value="<?php echo $img; ?>" name="images[]" id="hid_<?php echo $key; ?>">
<?php
}
}
?>
</div>
I want to allow the user to just add 3 or 4 images maximum. How to do that?

<?php $count=count($_FILES['name']);
if($count>3)
{
echo "Your error message";
}
else{
echo "your desired activity";
}?>

Using jquery you can call this function while submit your form
var file_nu = $("input:file")[0].files.length;
if(file_nu >3)//apply your condition here
{
return FALSE;
}else{
return TRUE;
}

Related

Session creation issue in shopping cart

In my code for shopping cart, sessions are not formed dynamically. I want to create session in this page and use sessions on the other page for item cart details.
How do I solve this problem?
<form method="post" action="men.php?action=add&id=<?php echo
$product['id']; ?>">
<img src="<?php echo $product['image'];?>">
<img class="hover-img" src="<?php echo $product['h_image'];?>" >
<p><?php echo $product['price'];?></p>
<h6><?php echo $product['name'];?></h6>
<input type="hidden" name="id" value="<?php echo $product['id']; ?>" />
<input type="hidden" name="hidden_name" value="<?php echo
$product['name']; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo
$product['price']; ?>" />
<input type="hidden" name="hidden_image" value="<?php echo
$product['image']; ?>" />
Quantity <input type="text" class="form-control" name="qty" value="1" /><br>
<input type="submit" name="cart" value="Add to Cart" />
</form>
<?php
session_start();
if(isset($_POST['cart']))
{
// not working
if(isset($_SESSION['shopping_cart']))
{
$count= count($_SESSION['shopping_cart']);
$product_ids= array_column($_SESSION['shopping_cart'],'id');
if(!in_array(filter_input(INPUT_GET,'id'), $product_ids))
{
$_SESSION['shopping_cart'][$count]=array
(
'id'=> filter_input(INPUT_GET,'id'),
'name'=> filter_input(INPUT_POST,'hidden_name'),
'price'=> filter_input(INPUT_POST,'hidden_price'),
'quantity'=> filter_input(INPUT_POST,'qty'),
'image'=> filter_input(INPUT_POST,'hidden_image')
);
}
else
{
for($i=0; $i<count($product_ids); $i++)
{
if($product_ids[$i]==filter_input(INPUT_GET,'id'))
{
$_SESSION['shopping_cart'][$i]['quantity']+=
filter_input(INPUT_POST,'quantity');
}
}
}
}
else
{
}
}
?>
Sara. The session_start(); should be the first line in your document.
So, it should look like:
<?php session_start(); ?>
and then the rest of your work
NOTE : IF IT STILL NOT WORKING PLEASE PROVIDE THE ERROR.

How to add/change fields dynamicaly in form from if else condition?

Here i have numerous input fields in a form, which are working perfectly. And now, i want to add new input boxes and their labels according to property_type , Which is already exist on this page.
Now, Kindly Suggest me How can i add new fields in that form.
Here is my Form Code
<div class="dashboard_price_left">
<h3>
<?php
if ($this->lang->line('BasePrice') != '') {
echo stripslashes($this->lang->line('BasePrice'));
} else
echo "Base Price";
?>
</h3>
<p>
<?php
if ($this->lang->line('Atitleandsummary') != '') {
echo stripslashes($this->lang->line('Atitleandsummary'));
} else
echo "Set weekly or monthly price guests will see for your listing.";
?>
</p>
</div>
<?php
if ($listDetail->row()->home_type == 'Office') {
} else if ($listDetail->row()->home_type == 'House') {
}
?>
<form id="pricelist" name="pricelist" action="site/product/savePriceList" method="post">
<div class="dashboard_price_right">
<label><?php
if ($this->lang->line('Pernight') != '') {
echo stripslashes($this->lang->line('Pernight'));
} else
echo "Per night";
?>
</label>
<div class="amoutnt-container">
<?php if ($currentCurrency == '') { ?>
<span class="WebRupee"><?php echo $currencyDetail->row()->currency_symbols; ?></span>
<?php } else { ?>
<span class="WebRupee"><?php echo $currentCurrency; ?></span>
<?php } ?>
<input type="text" id="price" value="<?php
if ($listDetail->row()->price != '0.00') {
echo intval($listDetail->row()->price);
}
?>" class="per_amount_scroll" name="price" onkeypress="return onlyNumbersWithDot(event);" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'price');" />
<input type="hidden" id="id" name="id" value="<?php echo $listDetail->row()->id; ?>" />
</div>
<div class="dashboard_currency">
<label><?php
if ($this->lang->line('Currency') != '') {
echo stripslashes($this->lang->line('Currency'));
} else
echo "Currency";
?>
</label>
<div class="select select-large select-block">
<select name="currency" id="currency" onchange="javascript:Detailview(this,<?php echo $listDetail->row()->id; ?>, 'currency');get_currency_symbol(this)" >
<!--<option value="">select</option>-->
<?php foreach ($currencyDetail->result() as $currency) { ?>
<option value="<?php echo $currency->currency_type; ?>" <?php if ($listDetail->row()->currency == $currency->currency_type) echo 'selected="selected"'; ?>><?php echo $currency->currency_type; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
</form>
Here is a pic of my Property prices form
Note: There are two types of property that are following:
1. Office
2. House
on the basis of these property, I want to add more fields in my form.
Please suggest me, Thanks in Advance :)
you can change it base on conditon and pass hidden field which indicates you office or house.
<?php
if ($listDetail->row()->home_type == 'Office')
{
<input type="hidden" name="home_type" value="Office" />
<input type="text" name="per_day" value="" />
<input type="text" name="half_day" value="" />
<input type="text" name="per_hour" value="" />
}
else if ($listDetail->row()->home_type == 'House')
{
<input type="hidden" name="home_type" value="House" />
<input type="text" name="per_night" value="" />
<input type="text" name="per_day" value="" />
<input type="text" name="per_hour" value="" />
}
?>

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

Not validating one of the search fields

I want to check if either of my search fields are empty, and if they are i write out a failure message. For some reason the following code doesn't work when I try to submit empty values in both search fields, or leaving the q2 search field empty. It only writes out the failure message when I only insert a value into the first field. Hope you guys can help.
<div id="searchField">
<form name="searchField" action="" method="get">
<input type="search" name="q" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="search" name="q2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>
<input type="hidden" id="v" name="v" value="2" />
<input type="hidden" id="b" name="b" value="" />
<button type="submit" id="loop" style="border:0px; background:transparent; vertical-align:middle; margin-left:-85px; margin-top:-3px;">
<img src="images/loop.png" width="75" height="75" alt="submit" />
</button>
</form>
</div>
<div id="theData">
<?
if(str_replace(" ", "",$_GET['q'])!="" && str_replace(" ","",$_GET['q2'])!=""){
$qContent = $_GET['q'];
$succes = mysql_query("SELECT*FROM film WHERE name='".$qContent."'");
include("searchMaster.php");
if(mysql_num_rows($succes)) {
while($o = mysql_fetch_array($succes)){
echo $o['name'];
if($o['trailer']){
echo '<div id="ytplayerWrap">';
echo '<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/'.$o['trailer'].'?http://amnestic.ueuo.com"
frameborder="0"/>';
echo '</div>';
}else {
echo '<span style="font-size:20px;">';
echo '<br /><br /> No trailer was found for <em>'.$_GET['q'].'</em> :(';
echo '</span>';
}
}
}else{
echo 'You did the fuckup :(';
}
}else{
echo 'You did the fuckup :(';
}
?>
The name and id of your input fields must be same
Like this
<input type="text" name="submitQuery" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="text" name="submitQuery2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>

Categories