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"];
}
Related
Sorry for my noobness here I'm trying to create a project with no PHP knowledge at all.
I have a database set up with a list of users and I am able to display users based on specific information through a search. Each search query has a checkbox. What I am trying to do now is limit the amount of selected checkboxes (up to 3 selections only) and save the selected queries to a different page. Can someone point me in the right direction? I'm sure my code is all over the place and probably wrong in many ways so I apologise in advance. I appreciate it.
Search Results Page:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/overallheader.php';
?>
<h1>New Staff Request</h1>
<p>Search for potential staff using the form below</p>
<form method="POST">
<input type="TEXT" name="search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
<br/>
<p><b> Select only up to 3 results</b></p>
<form method="post" action="StaffingRequest.php">
<?php
if(isset($_POST['submit'])){
$mysqli = NEW mysqli('localhost','root','','lr');
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM users WHERE jobcat LIKE '%$search%' ");
if($resultSet->num_rows > 0) {
while($rows = $resultSet->fetch_assoc()) {
$first_name = $rows['first_name'];
$last_name = $rows['last_name'];
$education = $rows['education'];
$salary = $rows['salary'];
$jobcat = $rows['jobcat'];
echo "<br /><input type='checkbox' name='query[]' value=''> First Name: $first_name<br />Last Name: $last_name<br />Job Category: $jobcat<br />Education: $education<br />Salary: $salary<br /><br />";
}
} else {
echo "No Results";
}
}
?>
<input type="submit" name="submit" Value="Save">
</form>
<?php include 'includes/overall/overallfooter.php'; ?>
Here is the page I want the results to display on:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/overallheader.php';
?>
<h1>My Staff Requests:</h1>
<p></p>
<?php
$chkbox = $_POST['query'];
$i = 0;
While($i < sizeof($chkbox)) {
echo "CheckBox Selected Values = " . $chkbox[$i] . '</br>';
$i++;
}
?>
<?php include 'includes/overall/overallfooter.php'; ?>
You are going to want to use JS/jQuery to accomplish this.
What you are going to do is add some client side functionality that checks how many check boxes of the same class have a check in them.
I added the code for you. This is what it does.
Add the jQuery library.
Add a classname (class="checkboxes") to the input tag to be referenced later.
Detect when there has been a change to an element with the class name "checkboxes".
Count to see how many elements with the class name "checkboxes" have a checked checkbox.
If there are three checked check boxes, remove the check from the last(4th) selected check box.
Display an error to the user that only three check boxes can be selected.
Like so:
<?php
include 'core/init.php';
protect_page();
include 'includes/overall/overallheader.php';
?>
<h1>New Staff Request</h1>
<p>Search for potential staff using the form below</p>
<form method="POST" action="">
<input type="TEXT" name="search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
<br/>
<p><b> Select only up to 3 results</b></p>
<form method="POST" action="StaffingRequest.php">
<?php
if(isset($_POST['submit'])){
$mysqli = NEW mysqli('localhost','root','','lr');
$search = $mysqli->real_escape_string($_POST['search']);
$resultSet = $mysqli->query("SELECT * FROM users WHERE jobcat LIKE '%$search%' ");
if($resultSet->num_rows > 0) {
while($rows = $resultSet->fetch_assoc()) {
$first_name = $rows['first_name'];
$last_name = $rows['last_name'];
$education = $rows['education'];
$salary = $rows['salary'];
$jobcat = $rows['jobcat'];
echo '<br /><input type="checkbox" name="query[]" class="checkboxes" value=""> First Name: ' . $first_name . '<br />Last Name: ' . $last_name . '<br />Job Category: ' . $jobcat . '<br />Education: ' . $education . '<br />Salary: ' . $salary . '<br /><br />';
}
} else {
echo "No Results";
}
}
?>
<input type="submit" name="submit" Value="Save">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".checkboxes").change(function (){
if($('.checkboxes:checkbox:checked').length > 3){
$(this).prop('checked', false);
alert("You can only select 3 checkboxes.");
}
});
});
</script>
<?php include 'includes/overall/overallfooter.php'; ?>
A couple other things.
I try real hard not to have more than one form on a page. For the most part you should only need one. I would say that if you have two forms it's a special case. Your first form that you use for the search could easily be replaced with an AJAX routine.
When echoing HTML though PHP, I recommend using single quotes. This will prevent alot of problems dealing with the quotes for the elements attributes. Here is an example of how I do it:
echo '<input id="myId">Test</input>';
This will save you some headaches down the road.
Hope this works for you..
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.
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";
}
}
}
?>
This question already exists:
form submit help me please [closed]
Closed 7 years ago.
I really need a help with this problem, being trying to fix it or find-out the solution, but couldn't find anything to help.
I have a website " www.encoregfx.com"
Under the page you'll see Share your idea, then Send your message.
When someone fill the questions, then press send your message it does not work.
How i can make it works by sending the message to my e-mail.
I will post the code here may there is something wrong or less.
<form class="testim-form" HREF="popupbasic.html" onClick="return popup(this, 'stevie')">
<div class="t_form">
<input class="t_form1" name="name" type="text" placeholder="الاسم" data-0="opacity:1;left:0%;" data-2600="left:0%" data-3000="left:100%;" data-2000="left:100%" data-2300="left:0%;">
<input class="t_form2" name="email" type="text" placeholder="البريد الالكتروني" data-0="opacity:1;left:0%;" data-2700="left:0%" data-3100="left:100%;" data-2050="left:100%" data-2350="left:0%;">
<textarea class="t_form3" name="testimonials" placeholder="نص الرسالة" data-0="opacity:1;left:0%;" data-2800="left:0%" data-3200="left:100%;" data-2100="left:100%" data-2400="left:0%;"></textarea>
<input class="t_form4" type="submit" name="ready" value="Send the message" data-0="opacity:1;left:0%;" data-2900="left:0%" data-3300="left:100%;" data-2150="left:100%" data-2450="left:0%;">
</div>
</form>
I hope if someone help me with that, the theme is made by html5.
The form just passes the parameters (name, email and testimonials) back to the web site in a GET http call like this:
http://www.encoregfx.com/?name=abc&email=abc&testimonials=abc&ready=blahblah
For it to actually send email the server would need to receive that set of parameters and act on it. The browser won't send anything on its own. What does your server do when it receives the request?
Look to add both a method and an action to your form like so, I have also provided the php submit file so you can see what details have been submitted. The data tags have be removed just to shorten the example.
form.html
<form action="submit.php" method="post">
<div>
<input name="name" type="text" placeholder="الاسم">
<input name="email" type="text" placeholder="البريد الالكتروني">
<textarea name="testimonials" placeholder="نص الرسالة"></textarea>
<input type="submit" name="ready" value="Send the message">
</div>
</form>
submit.php
<?php
$name = ( isset( $_POST['name'] ) ) ? $_POST['name'] : null;
$email = ( isset( $_POST['email'] ) ) ? $_POST['email'] : null;
$testimonials = ( isset( $_POST['testimonials'] ) ) ? $_POST['testimonials'] : null;
echo "name = " . $name . "<br>";
echo "email = " . $email . "<br>";
echo "testimonial = " . $testimonials;
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I have a PHP array:
foreach($xpath->query('//a') as $element) {
$linklabel[] = $element->textContent;
$link[] = $element->getAttribute("href");
$i=$i+1;
}
I also have an HTML form:
<form name="keypad" id="form" onSubmit="return pass(this)">
<input type="text" size="100%" length="25" value="" name="lcd">
<input type="button" value=" 1 " name="one" onClick="this.form.lcd.value+=this.value">
<input type="button" value=" 2 " name="two" onClick="this.form.lcd.value+=this.value">
<input type="button" value=" 0 " name="zero" onClick="this.form.lcd.value+=this.value">
<input type="submit" name="Submit" id="Submit" value="Go" >
</form>
How can I write the onSubmit function pass() that takes the values from the keypad and sends the corresponding $link[] value to browse.php. For example, if $link[20]='google.com'; , I want to pass $link[20] to browse.php if user enters 20 in the keypad and presses Go.
You'll need to output the PHP array in a way the Javascript can interpret, like as a Javascript array:
<?php
echo "<script type=\"text/javascript\">\n";
echo "var links = [";
foreach($links as $link)
echo "'$link', ";
echo "];\n";
echo "</script>\n";
?>
You also need an element in the form the function can set:
<input type="hidden" name="url">
Then your Javascript function can index into the array and modify the form element:
function pass() {
id = parseInt(document.getElementsByTagName('form')[0].lcd.value, 10);
document.getElementsByTagName('form')[0].url.value = links[id];
}
Your button values have spaces around them for some reason; they should just be the number, or lcd.value will end up with spaces throughout it. If you want the buttons to have padding use CSS.
Finally, you need to post the form if you're going to check $_POST on the other end, so change the tag to:
<form name="keypad" id="form" method="post" onSubmit="return pass()">
You can also just use $_GET on the target page