Submitting a PHP upload once files have been selected [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an image upload script using PHP with a simple multiple file select and then an upload function like below:
mysql_connect("localhost", "root", "") or die("error");
mysql_select_db("repo") or die("error");
$imgerror = '';
if(isset($_POST['log'])){
foreach($_FILES['files']['tmp_name'] as $key => $name_tmp){
$name = $_FILES['files']['name'][$key];
$tmpnm = $_FILES['files']['tmp_name'][$key];
$type = $_FILES['files']['type'][$key];
$size = $_FILES['files']['size'][$key];
$dir = "content/images/".$name;
$move = move_uploaded_file($tmpnm,$dir);
if($move){
$hsl = mysql_query("insert into files values('','$client','$name','$type','$size',now())");
if ($hsl){
$imgerror = "IMAGE(S) UPLOADED SUCCESSFULLY";
} else {
$imgerror = "CANNOT CONNECT TO DATABASE";
}
} else {
$imgerror = "NO IMAGES SELECTED";
}
}
}
HTML:
<div class="uploadContainer">
<div><i><?php echo $imgerror ?></i></div>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" name="log" value="Upload">
</form>
</div>
I did find some information using jQuery from another question here but have no idea how I would implement this into my code, or maybe someone could suggest an alternative. All I am trying to do is select the files, and make it automatically submit without pressing a submit button.
Any help would be appreciated,
Thanks

The onchange event works for inputs type file. Next code auto-submits "once files have been selected" (tested in Mozilla Firefox) :
<html>
<head>
<script type="text/javascript">
function on_change ()
{ alert( "File(s) chosen!" +
"\n\n" +
"Click to submit files to upload." );
document.getElementById( "frm" ).submit();
}
</script>
</head>
<body>
<form id="frm" action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple onchange="on_change()">
<input type="submit" name="log" value="Upload">
</form>
</body>
</html>
Of course, you will have to remove the JavaScript "alert" window, it's there to show that the "onchange" event works.

Related

Sending to checked checkbox which is linked to email [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I'm new to coding such as Javascript and PHP, I need to create a form that will be hosted on a site which on a submit will send an email dependent on which checkbox is ticked, the different checkboxes will go to different emails, would I need to use a PHP post for this and is there any need for Jquery at all? or could I just use PHP? would love the help. Thank you
<?php
$mail1 = "test#test.com";
$mail2 = "test2#test.com";
$mail3 = "test3#test.com";
if(isset($_POST["MainCB"])) {
$mail1 = $_POST["MainCB"];
}
if(isset($_POST["ITCB"])) {
$mail2 = $_POST["ITCB"];
}
if(isset($_POST["CateCB"])) {
$mail3 = $_POST["CateCB"];
}
if(isset($_POST['submit'])){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$MainCB = $_POST['MainCB'];
$ITCB = $_POST['ITCB'];
$CateCB = $_POST['CateCB'];
$subject = "Form submission";
$message = $first_name . " " . $last_name . " " . $MainCB . " " . $ITCB . " " . $CateCB;
}
?>
<html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Maintenance:<input type="checkbox" id="MainCB"><br>
IT: <input type="checkbox" id="ITCB"><br>
Catering: <input type="checkbox" id="CateCB"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
You can create a form like this
<form action="mail.php" method="POST">
<input name="checkbox1" value="email1#yahoo.com" type="checkbox"></input>
<input name="checkbox2" value="email2#gmail.com" type="checkbox"></input>
<button type="submit">Test</button>
</form>
And in your mailing script you can retrieve the checkbox value.
if(isset($_POST["checkbox1"])) {
$mail1 = $_POST["checkbox1"];
}

Submit arrays from form fields to database [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
There is a sewing management app am working on and on the order page, someone can have multiple orders, but all orders are to be invoiced using one reference code. First page, I collected basic details such as pricing, but on next page, each individual order has to be entered in more detail, a sample picture of the material, instructions and style code if any.
So I've been able to successfully create the page where the data entry person can add multiple details and even select whether there's a style or not, but am stuck in the database submission.
Remember, every other pages am working with, like staff management and others are working just fine, so this error of not submitting to the database is not relative to an external page included or any other thing except probably how the arrays are handled. Below is a breakdown of the code;
JavaScript:
<script>
function showfield(name,event){
if(name=='iHaveStyle') {
$(event).next('#div1').html('<label>Enter Style Code</label><br /> <input type="text" name="style[]" />');
} else{
$(event).next('#div1').html('');
}
}
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".form-group"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div style="margin-top:20px; border-top:1px solid #333333;"><label>Upload Sample Material</label><br /><input type="file" name="sample_material[]" style="width:200px; height: 40px;" /><br/><br/><label>Customer&#39s Requirement</label><br /><textarea name="cust_requirement[]" style="width:200px; height: 150px;" /></textarea><br/><br/><label>Do you have a style</label><br /><select name="sketch_code" id="sketch_code" onchange="showfield(this.options[this.selectedIndex].value,this)"><option value="">I don&#39t have a style code</option><option value="iHaveStyle">I have a style code</option></select><div id="div1"></div><br />Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
HTML:
<form action="orderadd1.php?id=<?php echo "".$order_id; ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<button class="add_field_button">Add another sub-order</button>
<div style="margin-top:20px;">
<label>Upload Sample Material</label><br />
<input type="file" name="sample_material[]" style="width:200px; height: 40px;" /><br/><br/>
<label>Customer's Requirement</label><br />
<textarea name="cust_requirement[]" style="width:200px; height: 150px;" /> </textarea><br/><br/>
<label>Do you have a style</label><br />
<select name="sketch_code" id="sketch_code" onchange="showfield(this.options[this.selectedIndex].value,this)">
<option value="">I don't have a style code</option>
<option value="iHaveStyle">I have a style code</option>
</select>
<div id="div1"></div>
</div>
</div>
<input type="submit" class="btn btn-lg btn-color btn-block" value="Continue Order" name="submit_val">
</form>
PHP Code:
<?php
$order_id = $_REQUEST['id'];
if (isset($_POST['submit_val'])) {
if(is_array($_POST['cust_requirement']))
{
for($i=0; $i < count($_POST['cust_requirement']); $i++ ) {
$cust_requirement = mysql_real_escape_string($_POST['cust_requirement'][$i]);
$style = mysql_real_escape_string($_POST['style'][$i]);
$folder = "sample_material/";
$extention = strrchr($_FILES['sample_material']['name'], ".");
$new_name = $order_id."-".$i++;
$sample_material = $new_name.'.jpg';
$uploaddir = $folder . $sample_material;
move_uploaded_file($_FILES['submit_material']['tmp_name'], $uploaddir);
$data_submit = $pdo->query("INSERT INTO `order_desc` (order_id, sample_photo_url, cust_requirements, style_code) VALUES ('".$order_id."', '".$uploaddir."', '".$cust_req."', '".$style."')");
}}
}
?>
The error it is showing is:
Warning: strip_tags() expects parameter 1 to be string, array given in C:\xampp\htdocs\tms\header.php on line 2
I checked the header.php and this is exactly what I have there:
<?php
$_POST = array_map('strip_tags', $_POST);
?>
I used this header on all my pages and they are fine.
array_map function applies strip_tags() to every element of an array - but the most possible reason of this is that your $POST looks like this ['test1',['test2','test3]], $POST[1] == ['test2','test3] is an array and strip tags cannot be applied. You can do it using loop and checking if item is not an array to be stripped, or use recursion to go all over you $POST array
Your POST superglobal contains arrays that is why you get a warning with array_map. Try replacing it with array_walk_recursive http://php.net/manual/en/function.array-walk-recursive.php

.HTML File has Password in URL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I usually code in Java, but I knew a little HTML, so I decided I would learn more. My problem is that I have a password field and a submit button. When I hit the button, it checks to see if the password is right, and then asks you what your name is. It then changes a text field to say You got it right, NAME. The thing is, when you hit submit, the code submitted is added to the URL, so if you type password as the password, ?password is added on to the URL. That is fine with me, but since the URL is changed, the page reloads, making the text field go back to normal. I am using Google Chrome. Is there anyway around this, or is it because I am running a .HTML file, not going to a website?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Ryan Club Homepage</title>
<script>
function codeEnter(){
var s = document.getElementById("in").value;
var correct = "lolliPiper5";
if(s === correct){
var name = prompt("What is your name");
document.getElementById("cde").innerHTML = "You got the password right!, " + name;
}
}
</script>
</head>
<body style="font-family:'Myriad Pro' ">
<form onsubmit="codeEnter();">
<input type="password" name="code" id="in">
<br />
<input type="submit" value="Ready!">
</form>
</body>
</html>
Thank you!
You need to use JavaScript / jQuery to prevent the form from submitting. I am using jQuery 2.1.1.
For password field let's assume it 123 for now.
The e.preventDefault() method stops the default action of an element from happening. Here it stops the submit button to submit the form to URL specified in form's action attribute.
$(document).ready(function(){
$("#name_container").hide();
$('#submit').on("click",function(e){
e.preventDefault();
$password = $('#password').val();
if($password == '123'){
$("#password_container").hide();
$("#name_container").show();
$("#result").html("");
}
else{
$("#result").html("Password is incorrect.");
}
$name = $("#name").val();
if($name != '' && $name != null ){
$("#form").hide();
$("#result").html("You got it right, "+$name);
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="page.html" method="post" id="form">
<div id="password_container">
Password: <input type="password" id="password" />
</div>
<div id="name_container">
Name: <input type="text" id="name" />
</div>
<input type="submit" id="submit">
</form>
<div id="result">
</div>
(Updated)
Here you go:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body style="font-family:'Myriad Pro' ">
<form id="form" method="post" action="#">
Password:
<input type="password" name="code" id="in">
<br />
<input type="submit" value="Ready!" id="submit">
</form>
<div class="ps"></div>
<script>
$(document).ready(function () {
$('#submit').on("click", function (e) {
e.preventDefault();
$password = $('#in').val();
if ($password == 'lolliPiper5') {
$name = prompt("Enter your name", "ACCESS GRANTED");
$(".ps").html("Welcome to the team, " + $name);
}
});
});
</script>
</body>
</html>
In your simplified (I hope) code you need at least set
<form onsubmit="return codeEnter()">
...
// and in the script
function codeEnter(){
var s = document.getElementById("in").value;
var correct = "lolliPiper5";
if(s === correct){
var name = prompt("What is your name");
document.getElementById("cde").innerHTML = "You got the password right!, " + name;
}
else return false; //do not submit
}
In the real world if you actually wanted to submit the password, hidden from the user you would change the form code to
<form onsubmit="codeEnter();" method="post">
By default the form submits data to the server via a GET request which causes the values to show in the url, thus this is usually only used for making queries such as page numbers (?page=num) etc (all insensitive data).
However, when you set method="post" the form sends data using a POST request which is invisible to the user and in some cases encrypted before sending and therefore much safer.
An example of a for using method="POST" can be found here

website upload file to directory [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create website to upload a file into a directory inside called "Uploads". I was wondering how to do this with HTML, JavaScript and/or PHP. I am new to website building. Thanks.
Try this:
<form id="form_request_file" method="post" action="index.php" enctype="multipart/form-data">
<table align="center" width="525" border="0">
<label for="uploaded" class="control-label col-sm-2">Upload File</label>
<input id="uploaded" name="uploaded" type="file"/>
<input value="Submit" name="submit" type="submit"/>
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_FILES['uploaded'])) {
$path = "uploaded_docs/";
$file_name = basename($_FILES['uploaded']['name']);
$target = $path . $file_name;
if (move_uploaded_file($_FILES["uploaded"]["tmp_name"], $target)) {
echo $file_name . " was uploaded";
} else {
echo "Could not upload";
}
}
}
?>

using javascript in php for php session [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
The Code is:
<?php include 'connection.php';
if($logged_in=='yes')
{
echo "hell yea!"; //just for test and it works
?>
<script type="text/javascript">
alert("test 1 2 3"); //works too.
$("#form1").hide(); //to hide login form, but doesn't work
</script>
<?php
}
?>
I think question is quite clear. What is the reason? Thanks in advance.
EDIT: The session events are work fine by connection.php. No problem logging in or logout etc.
Here is my HTML code:
<div id="form1" name="form1" >
<label for="username"></label>
<input type="text" name="username" id="username" placeholder="Username" class="textbox"/>
<label for="password"></label>
<input type="password" name="password" id="password" placeholder="Password" class="textbox"/>
<span id="submit" class="submit">Log in</span>
</div>
<div>
<span id="logged_in" class="logged_in">Hello user etc.</span> //normally it is hidden from css.
</div>
and here is how I login:
$("#submit").click(function(){
$.ajax({
type:"POST",
url:"login.php",
data: '{"username":"'+$("#username").val()+'","password":"'+$("#password").val()+'"}',
dataType: "json",
success: function(data) {
if(data.ok=="done!"){
$("#form1").hide();
$("#giris").show();
$("#giris").html('Hello '+data.name+' '+data.lastname+' My Account');
}
else if(data.ok=="No such user!")
alert("No such user");
else
alert(data.ok); //activation required
}
});
});
EDIT: alert() line. HTML code added. Jquery part is added. And this forum tells me my post is mostly code so I think I have type something here. And I think I just did.
Try this code. Hope it will work.
<?php include 'connection.php';
if($logged_in=='yes')
{
echo "hell yea!"; //just for test and it works
?>
<script type="text/javascript">
$(document).ready(function(){$("#form1").hide(); });
</script>
<?php
}
?>
where dou you get tehe value of the session????
<?php
session_start();
// store session data
$_SESSION['logged_in']='yes';
?>
then
<?php include 'connection.php';
//////if($logged_in=='yes')
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']=='yes')
{
echo "hell yea!"; //just for test and it works
?>
<script type="text/javascript">
$("#form1").hide(); //to hide login form, but doesn't work
</script>
<?php
}
?>
check this link: http://www.w3schools.com/php/php_sessions.asp
I hope this help you men!

Categories

Resources