PHP ifelse makes the post values to database null with upload - javascript

I try to make an upload form and post some values to database. I write this code, but when I echo it, it gives a result and when I check the database, I get values null - there are no values... the file uploaded and was renamed, but no values were send to database.
table name: companies
columns:
CO_ID
company_name
company_sign_1
company_sign_name_1
company_sign_name_p_1
company_sign_2
company_sign_name_2
company_sign_name_p_2
company_sign_3
company_sign_name_3
company_sign_name_p_3
company_logo_1
company_logo_2
company_logo_3
My code is:
<script type="text/javascript">
function showfield(name){
if(name=='company_sing_1'){
document.getElementById('div1').innerHTML='Manager Name 1:<BR><input type="text" name="company_sign_name_1" /><BR>Manager Position 1:<BR><input type="text" name="company_sign_name_p_1" />';
}else if(name=='company_sing_2'){
document.getElementById('div1').innerHTML='Manager Name 2:<BR><input type="text" name="company_sign_name_2" /><BR>Manager Position 2:<BR><input type="text" name="company_sign_name_p_2" />';
}else if(name=='company_sing_3'){
document.getElementById('div1').innerHTML='Manager Name 3:<BR><input type="text" name="company_sign_name_3" /><BR>Manager Position 3:<BR><input type="text" name="company_sign_name_p_3" />';
}
else document.getElementById('div1').innerHTML='';
}
</script>
<?php
$newfilenamepath = "$filepath_d" . "$filename_d";
$company_name= ($_POST['company_name'])?$_POST['company_name']:'';
$upload_type= ($_POST['upload_type'])?$_POST['upload_type']:'';
if ($upload_type = "company_sing_1") {
$company_sign_name_1= ($_POST['company_sign_name_1'])?$_POST['company_sign_name_1']:'';
$company_sign_name_p_1= ($_POST['company_sign_name_p_1'])?$_POST['company_sign_name_p_1']:'';
mysql_query("update companies set $upload_type='".$newfilenamepath."', company_sign_name_1='".$company_sign_name_1."', company_sign_name_p_1='".$company_sign_name_p_1."' where CO_ID='".$company_name."'");
} elseif ($upload_type = "company_sing_2") {
$company_sign_name_2= ($_POST['manager2'])?$_POST['manager2']:'';
$company_sign_name_p_2= ($_POST['manager2_pos'])?$_POST['manager2_pos']:'';
mysql_query("update companies set $upload_type='".$newfilenamepath."', company_sign_name_2='".$company_sign_name_2."', company_sign_name_p_2='".$company_sign_name_p_2."' where CO_ID='".$company_name."'");
} elseif ($upload_type = "company_sing_3") {
$company_sign_name_3= ($_POST['manager3'])?$_POST['manager3']:'';
$company_sign_name_p_3= ($_POST['manager3_pos'])?$_POST['manager3_pos']:'';
mysql_query("update companies set $upload_type='".$newfilenamepath."', company_sign_name_3='".$company_sign_name_3."', company_sign_name_p_3='".$company_sign_name_p_3."' where CO_ID='".$company_name."'");
} else {
mysql_query("update companies set $upload_type='".$newfilenamepath."' where CO_ID='".$company_name."'");
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
<select name="company_name">
<option selected="selected" disabled="disabled">Please Select Name Of Company</option>
<?php
$company_lists = mysql_query("SELECT * FROM companies ORDER BY CO_ID DESC");
while ($row_com_list = mysql_fetch_array($company_lists)) {
echo "<option value='" . $row_com_list['CO_ID'] . "'>" . $row_com_list['company_name'] . "</option>";
}
echo "</select>";
?>
</select><BR /><BR />
<input name="file" type="file" size="20" /><BR /><BR />
<select name="upload_type" onchange="showfield(this.options[this.selectedIndex].value)">
<option selected="selected" disabled="disabled">Please Select Type Of Upload</option>
<option value="company_logo">Add A Logo</option>
<option value="company_logo_2">Add Second Logo</option>
<option value="company_logo_2">Add Third Logo</option>
<option value="company_sing_1">Add Sign For Manager 1</option>
<option value="company_sing_2">Add Sign For Manager 2</option>
<option value="company_sing_3">Add Sign For Manager 3</option>
</select><BR /><BR />
<div id="div1"></div></BR></BR>
<input name="submit" type="submit" value="Upload" />
</form>
What is the problem and what can we do?
edit:
when I fill the form and echo it and break at the first if I get this:
echo $upload_type;
echo "//////";
echo $company_sign_name_1;
echo "//////";
echo $company_sign_name_p_1;
echo "//////";
echo $newfilenamepath;
echo "//////";
echo $company_name;
break;
company_sing_1//////Abdulrahman Nahhas//////General Manager//////uploads/9-20140920-0494829001411172658.png//////2
this mean it take the form but not send the values to mysql I test the echo in the second elseif it gives me nothing.

your $upload_type is company_sing_1 etc instead of company_sign_1 etc. You have no fields in your db named company_sing_1 etc... Fix the naming and you'll fix your problem.

Related

Use value from a form in a redirect button outside the form

Fetch selected value from dropdown which is in one form and onClick of the button outside the form send the value to php page
<div class="drpvendorname">
<font style="color: white;">
<label>Distribution Point:</label>
</font>
</div>
<select class="form-control" id="drpvendor" name="pointname" required="">
<option selected disabled>Choose distribution point</option>
<?php
$strSQL = "SELECT * FROM point_tbl ORDER BY pointname ASC";
$query = mysqli_query($db, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo '<option onClick="distribution('.$result['pointshortname'].')" value="' .$result['pointshortname'].'">'. $result['pointname'].'</option>';
}
?>
</select>
</form><!--form1 ends here-->
<form action="../customer/form.php"><!--form2 starts here-->
<button class="btn pos7" name="abc" method="GET" style="margin-left:5%;">New Customer</button>
</form><!--form2 ends here-->
<div class="dailybreakupbtn">
<input class="btn" type="submit" id="dailybreakupbutn" name="dailybreakup" value="Enter Daily Breakup" onClick="distribution(<?=$pointname?>)"/>
</div>
<?php
if(isset($pointname)){
?>
<script type="text/javascript">
function distribution(pointname){
var pointname;
window.location.href="dailybreakup.php?query=" +pointname;
}
</script>
<?php
}
?>
I have tried this sending selected dropdown box value to next page using function name distribution
I ended sending undefined to the next page.
can any one help me sending the selected value to the next page with out putting the button in the <form>
May this will be help you :)
Html code:
<form action= "" method= "post">
<select class="form-control drpvendor" id="drpvendor" name="pointname" required="">
<option selected disabled>Choose distribution point</option>
<?php
$strSQL = "SELECT * FROM point_tbl ORDER BY pointname ASC";
$query = mysqli_query($db, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo '<option value="'.$result['pointshortname'].'">'. $result['pointname'].'</option>';
}
?>
</select>
</form>
JQuery Code:
$(doucment).on('change','.drpvendor',function(){
var data=$(this).attr('selected','selected');
$.ajax({
url: "dailybreakup.php",
data:'query='+ data,
type: "POST",
success: function(data) {
window.location.href='customer/form.php';
}
});
});
Use code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> // please link the path or use cdn
<script type="text/javascript">
function call_me(){
var pointname=document.getElementById("drpvendor").value;
alert(pointname); // comment it after testing
window.location.href="dailybreakup.php?query=" +pointname;
}
</script>
<form>
<select class="form-control" id="drpvendor" name="pointname" required="">
<option selected disabled>Choose distribution point</option>
<?php
$strSQL = "SELECT * FROM point_tbl ORDER BY pointname ASC";
$query = mysqli_query($db, $strSQL);
while($result = mysqli_fetch_array($query))
{
echo '<option value="' .$result['pointshortname'].'">'. $result['pointname'].'</option>';
}
?>
</select>
<input type="button" onclick="call_me()"/>
</form><!--form1 ends here-->
Note : 1) Code is not tested
2) use your select id instead of your_select_id
3) check variable

Option Menu with 2 Values

I have the following option menu in a form that will insert the fields into a table:
<option value="">select staff</option>
<?php
do {
?>
<option value="<?php echo $row_Staff['Staff_Name']."||".$row_Staff['Email']?>">
<?php echo $row_Staff['Staff_Name']?></option>
<?php
} while ($row_Staff = mysql_fetch_assoc($Staff));
$rows = mysql_num_rows($Staff);
if($rows > 0) {
mysql_data_seek($Categories, 0);
$row_Staff = mysql_fetch_assoc($Staff);
}
?>
</select>
I have 2 fields from source table in value of option from technique explained in How to post two values in an option field?: Staff_Name and Email.
I am trying to insert both fields from the form into a table using:
<input type="hidden" name="Staff_Name" class="form-control" id="Staff_Name" value=<?php
$staff =$_POST['Staff_Data'];
$staff_name = explode("||", $staff);
echo $staff_Name[0];
?> />
and
<input type="hidden" name="Email" class="form-control" id="Email" value=<?php
$staff =$_POST['Staff_Data'];
$email = explode("||", $staff);
echo $email[1];
?> />
Unfortunately, I can see the 2 fields separated by "||" in the table if I insert the option menu value but cannot seem to insert Staff_Name or Email into individual fields. On insert both fields are blank. Any help would be appreciated.
Instead of combine staffname and staffemail in the dropdown value. Please staffname in dropdown value and staffemail in the property of dropdown and onchange of the dropdown set those values in the hidden inputs so you will easily get those values on the form submission.
Please go through below code and let me know if you have any query.
//Dropdown
<select id="ddStaff">
<option value="">select staff</option>
<?php
do { ?>
<option value="<?php echo $row_Staff['Staff_Name']; ?>" staff-email = "<?php echo $row_Staff['Email'];?>">
<?php echo $row_Staff['Staff_Name']?>
</option> <?php
} while ($row_Staff = mysql_fetch_assoc($Staff));
$rows = mysql_num_rows($Staff);
if($rows > 0) {
mysql_data_seek($Categories, 0);
$row_Staff = mysql_fetch_assoc($Staff);
}
?>
</select>
//Input hidden fields to store staff name and staff email
<input type="hidden" id="txtStaffName" name="txtStaffName">
<input type="hidden" id="txtStaffEmail" name="txtStaffEmail">
//Jquery code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#ddStaff").on('change',function(){
var staffName = $(this).val();
var staffEmail = $('option:selected', this).attr('staff-email');
$("#txtStaffName").val(staffName);
$("#txtStaffEmail").val(staffEmail);
});
});
</script>
Please check it on https://jsfiddle.net/z4a0fywp/
For testing purpose I have not made the inputs hidden in the fiddle.

Based on a forms input value, how 2 generate selections 4 a drop down menu w row/record data from a database that will prefill a form when selected?

I'm trying to figure out a way to pre fill a form with data with past form information submitted in the past.
I have a form and a database. In my form I have a input named email that holds the pre-loaded default value of logged in member's email address that is read-only.
PIC
http://oi57.tinypic.com/2iubb4j.jpg
How can I generate a selection under a drop down menu that when selected will pre-fill the form with row/record data from my database?
and how can I generate only the records that match the forms input value 'email' to the records with the same value under the 'email' column in the database?
I've been at it for weeks now and can not seem to find any sense of direction on how to achieve this. Can't really find any tutorials site, video, sample code or anything close on how to make this possible. Any help would be great...thanks for your help in advance.
FORM
</header>
<body>
<form action="/demoform/contact_form.php" class="well" id="contactForm" method="post" name="sendMsg" novalidate="">
<big>LOAD PAST ORDERS:</big>
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
<option value="xxx">SAMPLE SELECTION</option>
</select>
</br>
<input type="text" required id="mile" name="mile" placeholder="Miles"/>
</br>
<input id="email" name="email" placeholder="Email" required="" type="text" value="demo#gmail.com" readonly="readonly"/>
</br>
<input id="name" name="itemname" placeholder="ITEM NAME 1" required="" type="text" />
</br>
<input type="reset" value="Reset" />
<button type="submit" value="Submit">Submit</button>
</form>
</body>
</html>
PHP FILE
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($connection, DB_NAME);
if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}
echo 'Connected successfully';
if (isset($_POST['itemname'])){
$itm = $_POST['itemname'];
}
else {
$itm = '';
}
if($_POST['mile']){
$mi = $_POST['mile'];
}else{
echo "Miles not received";
exit;
}
if($_POST['email']){
$email = $_POST['email'];
}else{
echo "email not received";
exit;
}
$sql = "INSERT INTO seguin_orders (itemname, mile, email)
VALUES ('$itm', '$mi', '$email')";
if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
UPDATE: closest thing below so far...but wont work...not sure it covers the matching the email values portion...thanks anyways 'edcoder'
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
<?php
$query='select * from tablename';
$res=mysql_query($query);
while($row=mysql_fetch_array($res))
{
?>
<option value="<?php echo $row['feildname']; ?>"><?php echo $row['feildname']; ?></option>
<?php
}
?>
</select>
It seems that you're almost there.
Try this.
<select id="extrafield1" name="extrafield1">
<option value="">Please select...</option>
<?php
$email = $_POST['email'];
$query="select * from tablename WHERE email={$_POST['email']}";
$res=mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($res))
{
?>
<option value="<?php echo $row['fieldname']; ?>"><?php echo $row['fieldname']; ?></option>
<?php
}
?>
</select>

how to change form response into ajax response?

I have a form, that uses a php file. The form lets the user:
-input 3 text fields for Title/Price/Description
-Select an image for his product
-Choose a table from the dropdown list (options are the table values)
As you see on my code after the user press the submit button, the browser redirects to another page, saying that "1 records was adder successfully".
I want it to be like after the user clicks the submit button on the form, a (javascript/ajax) messagewill appear letting him know that the records was added successfully.
Sorry for long coding, I think you might need everything.
OPEN TO ANY SUGGESTIONS
form
<div id="addForm">
<div id="formHeading"><h2>Add Product</h2></div><p>
<form id = "additems" action="../cms/insert.php" enctype="multipart/form-data" method="post"/>
<label for="title">Title: </label><input type="text" name="title"/>
<label for="description">Desc: </label><input type="text" name="description"/>
<label for="price">Price: </label><input type="text" name="price" />
<label for="stock">Quan: </label><input type="text" name="stock" />
<p>
<small>Upload your image <input type="file" name="photoimg" id="photoimg" /></small>
<div id='preview'>
</div>
<select name="categories">
<option value="mens">Mens</option>
<option value="baby_books">Baby Books</option>
<option value="comics">Comics</option>
<option value="cooking">Cooking</option>
<option value="games">Games</option>
<option value="garden">Garden</option>
<option value="infants">Infants</option>
<option value="kids">Kids</option>
<option value="moviestv">Movies-TV</option>
<option value="music">Music</option>
<option value="women">Women</option>
</select>
<input type="submit" name="Submit" value="Add new item">
</form>
</div>
insert.php (used on the form)
session_start();
$session_id='1'; //$session id
$path = "../cms/uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$table = $_POST['categories'];
$title = $_POST['title'];
$des = $_POST['description'];
$price = $_POST['price'];
$stock = $_POST['stock'];
$sql="INSERT INTO $table (title, description, price, image, stock)
VALUES
('$title','$des','$price','$path$actual_image_name','$stock')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "<h1>1 record added into the $table table</h1>";
echo '<button onclick="goBack()">Go Back</button>';
with jquery its quite simple:
Just do the following:
1st thing, remove the type="submit" in your input and give it a unique identifier like this:
<input id="submit_form" name="Submit" value="Add new item">
2nd thing, in your javascript file, do:
$(document).ready(function(){
$('input#submit_form').on('click', function() {
$.ajax({
url: 'addnew.php',// TARGET PHP SCRIPT
type: 'post' // HTTP METHOD
data: {
'title' : $('input[name="title"]').val()
},
success: function(data){
alert(data); // WILL SHOW THE MESSAGE THAT YOU SHOWED IN YOUR PHP SCRIPT.
}
});
});
})
3nd thing, in you php file:
Do the same thing but just do:
die("1 record added into the $table table")

clicking on the select option how to show hidden field using javascript?

I am working on php how to show hidden field?
I have bp_scholarship_enq table in my database and this database i have occupation field i want to add new occupation in my database how i do it?
<script>
function showss(ids)
{
var idss=ids;
if(idss=="other")
document.getElementById(idss).style.display='block';
}
</script>
<?php
echo "<select name='occupation' id='link_block' value='Source' style='width:196%'>
<option>select occupation </otpion>";
$sql = "SELECT * FROM bp_scholarship_enq";
$info = mysql_query($sql);
while ($row = mysql_fetch_array($info))
echo "<option > '" . #$row["occupation"] . "'</option>";
echo '<option onClick="showss('.input_field.')">other</option>';
echo "</select>";
echo '<input type="hidden" name="other" id="input_field" />';
?>
Hidden fields are not meant to be visible. You could use a textbox and set its visiblity to false in css and make visible it on changing the select option.
<input type="text" name="other" id="input_field" style="display:none"/>
<select name='occupation' id='link_block' value='Source' style='width:196%' onChange="showText(this.selectedIndex);">
...............
...............
<option value="other">other</option>
</select>
<script>
function showtext(ind){
var selectBox = document.getElementById('link_block');
if(selectBox.options[ind].value=="other"){
document.getElementById('input_field').style.display = "block";
}else{
document.getElementById('input_field').style.display = "none";
}
}
</script>
Instead of
<input type="hidden" name="other" id="input_field" />
Make it:
<input id="otherOccupation" class="hide" name="other" id="input_field" />
With hide being a class that sets visibility:hidden;
Then have some jquery to remove that class when the user selects "other"
$("#otherOccupation").removeClass("hide")

Categories