how to get value of a <select> without <form> HTML markup - php

i never put too much thought on how the data would be submitted without using <form> . I initially just focused on the design of my form.
now, im having trouble trying to send POST data without having a <form> markup.
im using bootstrap framework.
also, how do i get the values in my <select> markup to be submitted. below is my HTML and PHP code.
below HTML
<div id="box1">
<input name="fname" type="text" placeholder="name">
</div>
<div id="box2">
<select id="food" name="food">
<option value="meat" name="nmeat"> meat </option>
<option value="fruit" name="nfruit"> fruit </option>
</select>
</div>
// ^ Above code is in a JQuery Conditional Statement
// When user select meat, <select id="meat"> will display else fruit
<div id="box3">
<!--MEAT-->
<select id="meat" name="topic">
<option value="hotdog" name="nhotdog"> hotdog </option>
<option value="sausage" name="nsausage"> sausage </option>
</select>
<!--FRUIT-->
<select id="fruit" name="topic">
<option value="apple" name="napple"> apple </option>
<option value="grapes" name="ngrapes"> grapes </option>
</select>
</div>
<div id="box4">
<textarea name="desc" rows="5" class="span11" placeholder="Description"></textarea>
<button name="submit" class="btn btn-large btn-block btn-primary" type="button">Submit</button>
</div>
below PHP
Im having a hardtime making the correct syntax in PHP, specially if the values being submitted is not in a <form>
<?php
if ( isset( $_POST['submit'] ) ) {
$name = $_POST["fname"]; //box1
$gender =$_POST["gender"]; //box2
$topic =$_POST["topic"]; //box3
$desc =$_POST["desc"]; //box4
//echo $msg;
$sql = "insert into z_form (name) values ('".$msg."')";
mysql_query( $sql );
} else {
echo "error";
}
?>

Just use the <form> tag, really. POST method requires a <form> tag. I don't see any valid reasons to exclude the <form tags from a form.
now, im having trouble trying to send POST data without having a markup. im using bootstrap framework.
Bootstrap doesn't restrict you from using <form> tags. :)
Wrap your form in <form tags, as follows:
<form action="somefile.php" method="post" name="myForm">
<!-- your form code here -->
</form>
Also, change your button from:
<button name="submit" class="btn btn-large btn-block btn-primary" type="button">Submit</button>
to:
<input type="submit" name="submit" value="Submit" class="btn btn-large btn-block btn-primary"/>
And finally, in your PHP code, you're doing:
$gender =$_POST["gender"];
But there's no input field with the name attribute gender. Either you should create one input field with that name, or remove the definition from your script.
That should fix the issues. Cheers!

Why would you not use a <form> ? all other methods consist of using jquery in combination with ajax, or just ajax for sending the values. But there shouldn't be a reason to not use a <form> tag.

I always thought that you have to use the -tag if you want to submit data just with submit button.
An alternative is to take a button and use javascript / jquery to read data from fields and submit them via get-parameters with an url.
However it seems you would like to go the classic / easy way. So you need the and define the method and action.
In addition a little hint: Try to use a php framework with mvc-(model view controller)pattern. When you use it in the right way it makes you easier to handle your code, brings predefined functions / classes and makes your functions against database safer. An example for such a framework would be codeigniter but there are many more...

First, you need to include form tags:
<form action="process.php" method="post">
//form content
</form>
Change your submit button. It's also good to change the name to somthing other than "submit" to avoid namespace conflicts:
<input type="submit" name="sub" class="btn btn-large btn-block btn-primary" value ="Submit" />
Also, give your selects different names (You have two with the name "topic"):
<select id="meat" name="meat">
<option value="hotdog" name="nhotdog"> hotdog </option>
<option value="sausage" name="nsausage"> sausage </option>
</select>
And then retrieve those values from $_POST:
$meat = $_POST['meat'];

Please see below code:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(button).click(function() {
var name=$("#name").val();
var frt=$("#fruit").val();
var gender = $("#gender").val();
var topic= $("#topic").val();
var descr= $("#description").val();
var dataString = 'name='+name+'fruit='+ frt+'gender='+gender+'topic='+topic+'desc='+descr;
$.ajax
({
type: "POST",
url: "ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#success").html(html);
}
});
});
});
</script>
You should add id in all dropdown fields.
also add which you want to view the show success or error result
<div id="success"></div>

In #box3 you have 2 selects with the same name.
Try something like:
<select id="meat" name="meatSelect">
<select id="fruit" name="fruitSelect">
And then:
$meatSelect = $_POST['meatSelect'];
$fruitSelect = $_POST['fruitSelect'];

Related

Post Check Box in PhP

i got this annoying problem i can't figure out how to solve
After a quick research i found those links bellow, which i could partially make work
verifying if checkbox is checked in php
How to read if a checkbox is checked in PHP?
This is my html:
<form method="post">
<!-- CHECK BOX TIPO DE PRODUTO (INTEIRO/PEDAÇO) !-->
<input type="checkbox" name="stipo" id="stipo" value="1" onchange="document.getElementById('tipo').disabled = !this.checked;">Tipo
<select name="tipo" id="tipo" disabled="false">
<option value="Inteiro">Inteiro</option>
<option value="Pedaço">Pedaço</option>
</select><br>
<input type="submit" href="/consulta/consulta_produto.php" class="loader" value="Consultar">
</form>
and this is my php:
if ($_POST['stipo'] == 1){
echo "123";
} else {
echo "456";
}
Because at the Html code im using form like this:
<form method="post">
</form>
and making the post with a submit button like that:
<input type="submit" href="/consulta/consulta_produto.php" class="loader" value="Consultar">
It doesn't work due to the Href at the submit button.
However, if i do the Html code like this:
<form action="/consulta/consulta_produto.php" method="post">
<input type="submit" class="loader" value="Consultar">
</form>
It will work. But, the problem of the form above is that my script stop working. I Cant use the method above because i need Href into the submit button, because when i submit the form, im loading the next page inside a Div
Just in case it helps, this is my Javascript and why i need the Href
<script type="text/javascript">
$(function() {
$('.loader').click(function(event) {
event.preventDefault(); // stop the link loading the URL in href
$('#content').load($(this).attr('href'));
});
});
</script>
Anyone got some idea on how to make the POST method works with href instead of action on the form?

Go to the url of the selection after submitting the form

I have the following code so depending on the selected option, the visitor is directed to a new page on change. But I want them to be directed to that page after they've clicked on the submit button, not immediately on change. How can I do that?
<script type="text/javascript">
function go()
{
window.location=document.getElementById("link").value
}
</script>
<form>
<select id="link" onchange="go()" > <option>DEFAULT</option>
<option value="page1.php">TITLE1</option>
<option value="page2.php">TITLE2</option>
<option value="page3.php">TITLE3</option>
</select>
<div class="submit"> <input name="submit" type="image" id="submit" src="images/getaquotebutton.png" height="93" width="259"/> </div><!--end of submit class-->
</form>
Many thanks in advance
This is simple:
Remove the onchange from here:
<select id="link" onchange="go()" >
Instead add:
<input onclick="go()" name="submit" type="image" id="submit" src="images/getaquotebutton.png" height="93" width="259"/>
Edit:
Just noticed the type is set to image, well if this doesn't work as intended, try to change the type to button and add image through css as:
<input onclick="go()" name="submit" type="button" id="submit" style="background-image: url('images/getaquotebutton.png'); height:93px; width:259px;" />
With jQuery you can do something like this:
$('#submit').click(function(
window.location=document.getElementById("link").value;
));
Of course you should remove the onchange.
$(function() {
$('form').on('submit', function() {
if($('#link').val() == page1.php) {
do redirect..
}
});
});
Do like this for each!

send one form and print the same form in another php

How to make this same form by clicking the submit button, you send the same form to another php by POST method form where the inputs type text appear filled and the select will be select with the same option selected that the first form. I hope understand my explanation.
HTML
<form id="formPOST" action="pr01post.php" method="POST" >
Nombre<input type="text" id="txtNombre" name="txtNombre"/><br>
Edad<input type="text" id="txtEdad" name="txtEdad"/><br>
Provincia<select id="selProv" name="selProv">
<option value='0'>Seleccione una provincia</option>
<option value='1'>Álava</option>
<option value='2'>Albacete</option>
<option value='3'>Alicante/Alacant</option>
</select><br>
Fec. Nac.:<input type="text" id="txtFecNac" name="txtFecNac"/><br>
Sexo: Hombre<input type="radio" name="radSex" value="Varón" checked>Mujer<input type="radio" name="radSex" value="Mujer">
<input type="submit" name="subEnv" />
</form>
Use the power of jQuery, Luke!
<script type="text/javascript">
$(document).ready(function() {
$('#selProv option[value="<?=$_POST['selProv'];?>"]').attr('selected','selected');
});
</script>
And be sure you've included actual jquery lib on your page. Can do it like this:
<script src="http://code.jquery.com/jquery-1.9.1.js" ></script>

reading from input tag outside form element

I have a web page with an input field (a calendar to select a date from).
The problem is that I need to read that input field from 2 different form elements that each have their own action and other values.
|calendar| [do this] [do that]
I don't want to copy the input field in both forms since it would look silly.
Any suggestions?
Edit: here's the two forms:
<form action="/adm/user/credit/update?sms=<?=$id?>&user=<?=$user?>" method="post">
<input type="hidden" name="isPaid" value="1"/>
<label for="datums">Datums: </label>
<input type="text" id="datums" name="datums"/>
<input type="submit" value="Paid" style="background-color:green; color:white"/>
</form>
<form action="/adm/user/credit/extend?sms=<?=$id?>&user=<?=$user?>" method="post">
<input type="hidden" name="phone" value="<?=$phone?>">
<select name="period">
<option value="7">7</option>
<option value="14">14</option>
<option value="30">30</option>
</select> days
<input type="submit" value="Extend" style="background-color: blue; color: white">
</form>
Just give it unique ID and use document.getElementById like this anywhere you want:
var myValue = document.getElementById("myInputID").value;
This way it doesn't matter where the input is located, it can even be outiside of any form.
Edit:
To read the value upon submit, first add the onsubmit part to the relevant form:
<form action="....." onsubmit="ReadValue(this);">
And now have this in your page:
<script type="text/javascript">
function ReadValue(oForm) {
var myValue = document.getElementById("myInputID").value;
alert("value is: " + myValue);
}
</script>
This will show the value as alert dialog.. if you want to populate hidden form field with the value, have such code instead:
function ReadValue(oForm) {
var myValue = document.getElementById("myInputID").value;
oForm.elements["myHiddenFieldId"].value = myValue;
}
Hope it's clear enough! :)

I need to have a form be filled out, go to a page chosen in a drop down then use the other info from the form to populate the chosen page

I have asked two previous questions for taking info put into a form and using it to populate a page, and one to redirect you to a page when you submit the form based off of what you choose in the forms drop down. Now I need these two things to both work for the same form.
The code for taking the info from the form and using it to populae the next page is:
<?php
$firstname = $_GET['personsName'];
echo "My Name is" .$firstname;
?>
And the form would look like this:
<form action="letter.php" method="get">
<input type="text" name="personsName"></input>
<input type="submit" value="submit">
</form>
The code for selecting a page is:
$pages = array('Page 1' => 'page1.php', 'Page 2' => 'page2.php', 'Page 3' => 'page3.php');
if (array_key_exists($_POST['dropdown-name'], $pages)) {
header("Location: " . $pages[$_POST['dropdown-name']]);
} else {
echo "Error processing form"; // submitted form value wasn't in your array, perhaps a hack attempt
}
I need for both of these to work for the same form, I just haven't been able to figure it out. What I tried was:
<form action="<?=$pages?>" method="POST">
<input type="text" name="name" /><br />
<select name="letter">
<option value="Page 1">
Page 1
</option>
<option value="Page 2">
Page 2
</option>
<option value="Page 3">
Page 3
</option>
</select>
<input type="submit" value"Print" />
</form>
This is obviously wrong. I need this form to take the info put into it, redirect to the page chosen, then populate the page with the other info inputted into the form. The problem is I have no idea how to get the answers I already have together. Thank you to all that help!
Current code is this:
<script type="text/javascript">
$(function UpdateFormAction(){
alert('Launched event handler');
var form = document.getElementById('MyForm');
var list = document.getElementById('PageList');
alert('List item numer ' + list.selectedIndex);
var desiredAction = list.options[list.selectedIndex].value
alert('Desired action set to ' + desiredAction);
form.action = desiredAction;
});
</script>
Form is:
<form id="MyForm" action="letter.php" method="POST">
<input type="text" name="name" /><br />
<select id="PageList" name="letter" onchange="UpdateFormAction();">
<option value="letter.php">Page 1</option>
<option value="letter2.php">Page 2</option>
<option value="letter3.php">Page 3</option>
</select>
<input type="submit" value"Print" />
</form>
You're losing the data sent to the server when you do the redirect.
Basically, you've got 3 options:
Store the data on the client using a cookie or similar (A lot of overhead for something simple)
Store the data server-side in a session or similar (even more overhead)
Pass the data through the URL you're redirecting to...
So something like:
if (array_key_exists($_POST['dropdown-name'], $pages)) {
header("Location: " . $pages[$_POST['dropdown-name']] . "?personsName=" urlencode($_POST['name']));
} else {
//Blah
}
Which seems to be the best answer for your simple example. If there's a LOT of data or it's more complex, consider using one of the other 2 option above
NB: On the form you've called the variable name, on the pae receiving it, you've called it personsName - note the mapping from one to the other in the URL.
Javascript solution (tested):
<html>
<head>
<title>SO Demo</title>
</head>
<body>
<script type="text/javascript">
function UpdateFormAction(){
var form = document.getElementById('MyForm');
var list = document.getElementById('PageList');
var desiredAction = list.options[list.selectedIndex].value
form.action = desiredAction;
document.getElementById('Target').innerHTML = 'Form action set to: ' + desiredAction;
}
</script>
<form id="MyForm" action="letter.php" method="POST">
Name: <input type="text" name="name" /><br />
Page: <select id="PageList" name="letter" onchange="UpdateFormAction();">
<option value="letter.php">Page 1</option>
<option value="letter2.php">Page 2</option>
<option value="letter3.php">Page 3</option>
</select>
<input type="submit" value="Print" /><br/>
<span id="Target">Form action unmodified</span>
</form>
</body>
</html>

Categories