I am attempting to build a simple contact form inside of a function so that I may add it as a shortcode later. I wish to change the mailto address based on a "select" value.
function contact_form_function() {
switch($_POST['selectedValue']){
case 'webmaster':
$mailbox='webmaster#email.com';
break;
case 'careers':
$mailbox='careersk#email.com';
break;
case 'projects':
$mailbox='projects#email.com';
break;
case 'info':
$mailbox='info#hotmail.com';
break;
default:
// Something went wrong or form has been tampered.
}
?>
<form action="MAILTO:<?php echo($mailbox); ?>" method="post" enctype="text/plain">
<input type="text" name="name" required> Name(required)
<input type="text" name="mail" required> Mail(will not be published, required)
<input type="text" name="website"> Website
<select name="selectedValue">
<option value="webmaster">Website Comment</option>
<option value="careers">Careers Information</option>
<option value="projects">Project Opportunity</option>
<option value="info">Other</option>
</select>
<textarea></textarea>
<?php
echo($mailbox);
?>
<input type="submit"></submit>
</form>
<?php
}
?>
Its not echoing anything back and not changing the email address so I know I've done something wrong. I just don't know what. I'm a novice when it comes to coding so any advice is helpful.
You need to set the $mailbox value before you try to use it. I recommend using jQuery.
<script src="jquery.js"></script>
<script type="text/javascript">
jQuery.noConflict();
(function ($) {
function readyFn() {
$("#emailSelect").change(function(){
$("#your_form").attr('action', 'MAILTO:' + $("#emailSelect").val() + '#email.com');
});
}
$(document).ready(readyFn);
})(jQuery);
/*$(document).ready(function(){
$("#emailSelect").change(function(){
$("#your_form").attr('action', 'MAILTO:' + $("#emailSelect").val() + '#email.com');
});
});*/
</script>
...
<form action="" id="yoor_form" method="post" enctype="text/plain">
...
<select name="selectedValue" id="emailSelect">
<option value="webmaster">Website Comment</option>
<option value="careersk">Careers Information</option>
<option value="projects">Project Opportunity</option>
<option value="info">Other</option>
</select>
</form>
You need to set the $mailbox value before you try to use it.
And if you want something to happen in the browser when the user selects an option in the drop down, you have to use javascript, not PHP.
You are defining $mailbox after you try to use the value.
You have to have your $mailbox = ... stuff before your <form>.
Related
I am doing an assignment for Uni. It requires me to make an HTML form that needs a user to input what their name is, what table number they're sitting on, if they're a regular customer, and their actual coffee order.
I have done this so far, but now I'm asked in the assignment to create a PHP file to make use of this information. I'm not too savvy with PHP and my lecture notes are doing nothing but confusing the hell out of me.
I have a rough idea about GET methods and POST, but how would I do the button coding? The 'show order' button should return a page of the customers' order. I have tried to read plenty of tutorials but this has sadly become my last resort and really hope someone can help me out with proper explanation.
If I'm not clear with anything here please do let me know I will explain it a little more, and no, I'm not asking for anybody to do the code for me, I just need a head start so i know how to start coding it.
My HTML code:
<!doctype html>
<html>
<head>
<title>Task 1 </title>
<style>
</style>
</head>
<body>
<center> <h1>Legendary Ice Creams </h1> </center>
<h4>Customer Information</h4>
<form action"task1.php" method = GET>
Customer Name: <input type="text" name="Your Name" title="Please enter your name here" placeholder="Your name here"><br>
Table number: <select name="Table number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select><br>
Regular Customer: <input type="checkbox" name="regularcustomer"
value="regularcustomer"><br>
<h4> Orders </h4>
<label for = "mulList[]"> Select one or more items</label>
<select id = "mulList[]" name = "mList[]" size = "4" multiple
= "true">
<option value = "First">First</option>
<option value = "Second">Second</option>
<option value = "Third">Third</option>
<option value = "Fourth">Fourth</option>
</select><br>
Select Coffee: <input type="radio" name="coffee">Latte</input>
<input type="radio"
name="coffee">Cappacino</input>
<input type="radio" name="coffee">Flat Half
De-Caf</input><br>
<button type="button" name="showOrder" action="task1.php">Show Order</input>
<button type="button" name="cancelOrder" action="task1.php">Cancel Order</button>
</form>
First of all, your form tag is wrong, the = is missing and the method should be enclosed in quotes too, like this:
<form action="task1.php" method="GET" enctype="multipart/form-data">
Now your actual problem: To give something to another php file, you need to name the HTML element with the name attribute. Then you can call it in this php file with $_GET or $_POST, depending in what you specified in the form tag. So to access your information, in my case just the name, this would be necessary:
$name = $_GET['Your Name'];
Then you can use it however you want. You can also use an easy loop to get all necessary information, the keys will be preserved and the values filtered with filter_var:
foreach($_GET as $k => $v) {
$getvars[$k] = filter_var($v);
}
I hope this covers your actual question, you seemed pretty lost to me, so i tried to explain it as easy as possible.^^
A little sample of an easy form with html/php:
htmlform.html
<html>
<head><meta charset="utf-8"></head>
<body>
<form action="phpfile.php" enctype="multipart/form-data" method="GET">
<input type="text" name="fname">
<input type="text" name="lname">
<input type="submit">
</form>
</body>
</html>
phpfile.php
<?php
$fname = filter_var($_GET['fname']);
$lname = filter_var($_GET['lname']);
echo "Your name is ".$fname." ".$lname;
?>
First of all do not use space inside the name attribute of your HTML elements. Either replace it an underscore or club the two words together.
Inside your task1.php, you can capture the user input by using the $_POST super variable. So $_POST['Your_Name'] will contain whatever the user typed inside the Customer Name textbox. You can have information about all the keys that you need by using:
echo '<pre>';
print_r($_POST); // replace this with $_GET if using method="GET" in your form tag
echo '</pre>';
in your task1.php page. I suggest you start by doing this and take it from there.
Also, remove the action attribute from your <button> tags.
First of all don't use space between names like Your Name in customer name better use Your_Name or anything you want
second add = between action and "task1.php"
and in
<button type="button" name="showOrder" action="task1.php">Show Order</input>
type will be submit i.e
<button type="submit" name="showOrder" action="task1.php">Show Order</input>
task1.php
if(!empty($_GET)){
echo "Customer Name:".$_GET['name'];//Printing Name
...
}
There is a "=" missing.
<form action"task1.php" method = GET>
should be
<form action="task1.php" method = "GET">
right?
Then you should be able to print the selected content in your
task1.php
<?php
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>
Correct?
I'm building a bug tracker tool.
When you create a project, you can change the project status (open, in progress & finished) on the project page using this select form:
<form action="classes/projectStatus.class.php" method="post">
<label> Change Project Status </label>
<select name="status" id="status">
<option value="Open">Open</option>
<option value="In Progress">In Progress</option>
<option value="Finished">Finished</option>
</select>
<input type='hidden' name='hdnID' value="<?php echo $id;?>">
<input class="small button" value="Change Status" type="submit">
</form>
This is the projectStatus.class.php file:
$status = $_POST['status'];
$id = $_POST['hdnID'];
$sql="UPDATE projects SET status = '$status'";
$result = mysql_query($sql);
$result = mysql_real_escape_string($sql);
if($result){
header('Location: ../projectpage.php?id='.$id);
} else {
echo "There is something wrong. Try again later."; }
mysql_close();
How can I do this with AJAX?
Could anybody provide me with some right code?!
I know the form isn't sql injection proof and I don't use mysqli, I will change this, but first I'd like to have an answer :).
Thanks!
I would use jquery, which will solve a lot of compatibility issues.
There is example code here on the jquery site: http://api.jquery.com/jQuery.post/
You'll want to use jQuery's $.post() function. You should check out the docs here for a ton of examples. So what you could do is change
<form action="classes/projectStatus.class.php" method="post">
to
<form onsubmit="return submit();">
and then define submit as
function submit()
{
$.post('classes/projectStatus.class.php', {
"status": $('#status').val(),
"hdnID": $('#hdnID').val()
}, function(response) {
//your callback function
});
return false;
}
This code is untested and might not work, but it's just meant to give you an idea of what to do. Let me know if this makes sense or if you have any questions :)
hy, i have a problem with a form. i know the question is simple but i can not have a solution. Well, this is my form:
<form id="search" method="post" action="cerca_redirect2.php" >
<select id="tipo" name="tipo"class="chzn-select" style="width:165px;" tabindex="1" >
<option value="http://case.vortigo.it/vendita-immobili/index.php"> Vendita</option>
<option value="http://case.vortigo.it/affitto-immobili/index.php">Affitto</option>
</select>
<input id="field" name="field" type="text" value=""/>
<input id="submit" type="submit" value="" />
</form>
my goal is when i select "Vendita" and i submit the form i have to go to the url in the select "Vendita", for each select. someone can help me? thanks
In the server side php code, do something like this
if (isset($_POST['tipo']) && !empty($_POST['tipo']))
{
header('Location: ' . $_POST['tipo']);
}
Note: This is a very basic version, you will want to ensure the url is valid by either maintaining a list of urls on the server, or something similar.
There are many different ways, but you can for example use following:
See the onsubmit part in the form definition
<form id="search" method="post" action="cerca_redirect2.php" onsubmit="this.action=document.getElementById('tipo')[document.getElementById('tipo').selectedIndex].value" >
If I understan your question correctly, you need a way to change the action value to the selected option's value
this is how to do that
$(document).ready(function(){
$("#tipo").on("change", function(){
$("#search").attr("action", $(this).val());
});
});
DEMO: http://jsfiddle.net/6ybMP/
You want to change the action of the form based on the select? The following should be along the lines of what you want.
$('#tipo').on('change', function() {
var newAction = this.val();
$('#search').prop('action', newAction);
}
UPDATE
You will want to wrap this code in $(document).ready() so that the event will be registered after the DOM has been loaded.
Like so:
$(document).ready(function() {
$('#tipo').on('change', function() {
var newAction = this.val();
$('#search').prop('action', newAction);
}
});
I have a select multiple list that has a few items in it. It is a list of IP addresses for an ACL. People can add/remove IPs, and then save the list. However, unless you select an item on the list, $_POST[selectName] does not contain any values. How can I accomplish this? I know I can do this with javascript but I would rather stick to PHP.
Edit/corrected: You need JS. There is no way to send all (selected and not selected) options via POST. You have to programatically select all options before submission.
File with form (file1.php):
<script type="text/javascript">
function selectAll()
{
selectBox = document.getElementById("someId");
for (var i = 0; i < selectBox.options.length; i++)
{
selectBox.options[i].selected = true;
}
}
</script>
<form method="post" action="file2.php">
<select id="someId" name="selectName[]" multiple>
<option value="123.123.123.123">123.123.123.123</option>
<option value="234.234.234.234">234.234.234.234</option>
</select>
<input type="submit" name="submit" value=Submit onclick="selectAll();">
</form>
File that receives POST (file2.php):
<?php
foreach ($_POST['selectName'] as $item)
{
print "$item<br/>";
}
?>
Just to tack on this you could also use the jQuery version of #Kamil's code which is a little simpler than the loop:
<script type="text/javascript">
jQuery('[name="form1"]').on("submit",selectAll);
function selectAll()
{
jQuery('[name="selectName[]"] option').prop('selected', true);
}
</script>
<form name="form1" method="post" action="file2.php">
<select id="someId" name="selectName[]" multiple>
<option value="123.123.123.123">123.123.123.123</option>
<option value="234.234.234.234">234.234.234.234</option>
</select>
<input type="submit" name="submit" value=Submit onclick="selectAll();">
</form>
I have got a form (php in html or the other way around). Once user selects an option in a drop down list, it would get the input value and create a few text boxes. It seems like I have to use onchange(). How do I read the input and perform logics within the script inself? Instead of opening another .php script?
Currently this is what I have.
<?php
$tables = $_POST["tables"];
?>
<html>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Table Name: <div id="tables">
<select name="tables">
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</div>
</form>
<?
echo "".$tables."";
?>
You can't interact with PHP once the HTML is sent to the browser without either
Refreshing the page, or
Using AJAX (JavaScript).
If you know the options in the <select> beforehand (which it seems like you do), you should write some JavaScript to accomplish what you need. Here is a simple example using jQuery.
$('#tables_select').change(
function( eventObject ){
alert('You chose ' + $(this).val());
switch( $( this ).val())
{
case 'Applications':
$('#tables').append('<input type="text" name="application_name" value="Enter an application name" />"');
break;
case 'Device':
$('#tables').append('<input type="text" name="device_name" value="Enter a device name" />"');
break;
}
}
);
You will need to add additional logic to remove the inserted elements if the user changes their choice, and to insert the correct <input> elements when the page first loads, but it is a good starting point.
if you want to add any input type ... here is the demo demo with code
you use following method.
<form method="post" action="<?php echo $PHP_SELF;?>" onChange="return createTxtbox()">
Table Name: <div id="tables">
<select name="tables">
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</div>
<span id="fooBar"> </span>
</form>
then write javascript,
<SCRIPT language="javascript">
function createTxtbox() {
var type="text";
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
}
</SCRIPT>