i have problem passing data from one page to another using GET, for example
i have these:
<form method=post action=edit.php>
<td><input type=text name=firstname></td>
<td>
<?
$query="SELECT * FROM gender;
$result = mysql_query ($query);
echo "<select name=gender_id>";
while($nt=mysql_fetch_array($result))
{
echo "<option value=$nt[gender_id]>$nt[gender_name]</option>";
}
echo "</select>";
?>
</td>
<td><input type=submit name=edit></td>
</form>
now to pass these to edit.php using GET
if($mode=="edit")
{
$fistname=$_GET["fistname"];
$gender=$_GET["gender_id"];
<td><input type=text name=firstname value="<? echo $fistname; ?>"></td>
Above is a working code for an input type text. I know how to pass values with input type text but my problem is that HOW WOULD I DO THESE WITH A SELECT tag WHICH HAS VALUES FROM A MYSQL DATABASE?.
<?
$query="SELECT * FROM gender;
$result = mysql_query ($query);
echo "<select name=gender_id>";
while($nt=mysql_fetch_array($result))
{
if ($nt[gender_id]==$_POST["gender_id"])
$selected="selected";
else
$selected="";
echo "<option ".$selected."value=$nt[gender_id]>$nt[gender_name]</option>";
}
echo "</select>";
?>
you were using <form method=**post** action=edit.php>, in that way you should use $fistname=$_POST["fistname"]; $gender=$_POST["gender_id"];
if you want to use $fistname=$_GET["fistname"]; $gender=$_GET["gender_id"];
you should use <form method=**get** action=edit.php but in that way, the thoses values will be visible in the url.
You have a typographical error on your edit.php change $_GET['fistname'] to $_GET['firstname']. Then edit:
<form method=post action=edit.php>
and put method="get"
<form method=post action=edit.php method="get">
And to prevent errors on edit.php. Add these:
if ((isset($_GET['firstname']) && (isset($_GET['gender_id'])) {
//your code
} else {
//some error message
}
<form method="GET" action="edit.php">
Or use $_POST in the script instead.
EDIT, since you didn't bother putting your question in the question:
As you iterate through the options, test the value of $nt[gender_id] and add the selected attribute to the one that matches.
Related
I am pretty much new to php and mysql .I am trying to create a php form
which data will be stored in backend Mysql database
I have created two php files
Form.php
insert.php
And both these files have an require_once(DB_Login.php) function which
connects to the Mysql DB
The contents of Form.php---
<html>
<head>
<body>
<form action="insert.php"
method="POST">
<?php
require_once('DB_Login.php');
$sql=mysql_query("Select * from Department");
echo '<table border=1px>';
echo '<th>DepartmentId</th> <th>Department_Name</th><th>
Description</th>';
$datas=array();
while($data=mysql_fetch_array($sql))
{
$datas[]=$data;
//Running a loop all contents in the Mysql Table
echo '<tr>';
echo '<td>.$datas[\'Dept_Id\'] </td> <td>.$datas[\'Dname\']</td>
<td>.$datas[\'Description\']</td>';
echo '</tr>';
}
echo'<tr>';
echo'<td><input type=\"text\" name=\"Dept_Id\" id=\"Dept_Id\"></td><td>
<input type=\"text\" name=\"Dname\" id=\"Dname\"></td> <td><input
type=\"text\" name=\"Description\" id=\"Description\"></td>';
echo'</tr>';
echo '</table>';
echo'<input type="submit" value="SEND">';
?>
</form>
</body>
</head>
</html>
The contents of insert.php--
<?php
require_once('DB_Login.php');
$Didtext = mysql_real_escape_string($_POST['Dept_Id']);
$Dnametext=mysql_real_escape_string($_POST['Dname']);
$Desctext=mysql_real_escape_string($_POST['Description']);
echo $Didtext;
echo $Dnametext;
$adddept="INSERT INTO Department('Dept_Id','Dname','Description')
values('$Didtext','$Dnametext','$Desctext')";
$result = mysql_query($adddept);
if($result)
{
echo("<br>Input data is succeed");
}
else
{
echo("<br>Input data failed");
}
?>
The problem think is values are not being passed to insert.php
$Didtext = mysql_real_escape_string($_POST['Dept_Id']);
$Dnametext=mysql_real_escape_string($_POST['Dname']);
$Desctext=mysql_real_escape_string($_POST['Description']);
After i run this code I get the message Input data failed.
I did a lot of search for this question .
I am using id attribute to pass element value to insert.php
Please help !!!
You should not use apostrophe (') when labeling your column names. Remove them.
Your insert query should look like this:
$adddept="INSERT INTO Department(Dept_Id, Dname, Description)
VALUES('$Didtext','$Dnametext','$Desctext')";
Also, make sure that your $_POST values from your form is the same with the name HTML tags of your input fields. You don't need to use backslash (\) when assigning a name tag on your input fields in your case, where you are using ' to echo and you are using " on assigning HTML tags.
<input type="text" name="Dept_Id" id="Dept_Id">
And double check the table name and column names that you use in your queries, because these data are case sensitive.
Note: You shouldn't be using the deprecated mysql_* API anymore and instead use mysqli_* prepared statement.
UPDATE:
Change your Form.php code as:
...
...
echo '<tr>';
echo '<td><input type="text" name="Dept_Id" id="Dept_Id"></td>';
echo '<td><input type="text" name="Dname" id="Dname"></td>';
echo '<td><input type="text" name="Description" id="Description"></td>';
echo '</tr>';
echo '</table>';
No need to write " \" before double quotes.
You did not pass the variables in which you store the value. Do the below change in insert.php:
$adddept="INSERT INTO Department('Dept_Id','Dname','Description')
values('".$Didtext."','".$Dnametext."','".$Desctext."')";
NOTE: If Dept_Id is int then no need to use ' before and after the variable $Didtext as :
$adddept="INSERT INTO Department('Dept_Id','Dname','Description')
values(".$Didtext.",'".$Dnametext."','".$Desctext."')";
This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";
I'm new to PHP-to-Mysql so I'm using the mysql_* function which others have stated to be unsupported anymore. But I find this place the best to ask my question since I can't find anyone with a similar question.
So basically, my problem is how to make a dropdown list stay which is inside an IF/ELSE block. When it is passes a value (submit) through another IF/ELSE block, it basically disappears so I'm trying a code where I don't have to re-write the entire dropdown code inside the next IF/ELSE block. Since what I'm trying is to show a textbox depending on what option is selected inside the dropdown (it has an auto-submit function). Is this possible? Or I have to resort to other libraries (jQuery,javascript etc.)?
Here's a part of the code if it helps:
elseif($_POST['question'] == edit ){
$res = mysql_query("SELECT questionID FROM questions");
echo "<form action='' method='post'>
Edit Question No.
<select name='question_select' onchange=this.form.submit()>
<option value=null selected>--</option>";
while($row = mysql_fetch_array($res))
{
echo "<option value=\"".$row['questionID']."\">".$row['questionID']."</option>";
}
echo "</select></form>";
}
//TEXTBOX TO BE DISPLAYED WHEN A NUMBER IS SELECTED IN EDIT EXISTING
if (isset($_POST['question_select'])){
$res = mysql_query("SELECT question FROM questions WHERE questionID='{$_POST['question_select']}'");
$row = mysql_fetch_assoc($res);
echo "Editing Question ",$_POST['question_select'],"<br>
<form action='' method='post'>
<textarea name='edited_question' rows='4' cols='50'>",$row['question'],"</textarea><br>
<input type='submit' name='save' value='Save'>
<input type='hidden' name='question_num' value='{$_POST['question_select']}'>
<input type='submit' name='cancel' value='Cancel'>
</form>";
}
//PASSING OF VALUES WHEN SAVE IS PRESSED
if (isset($_POST['save'])){
$edited_question = trim($_POST['edited_question']);
mysql_query("UPDATE questions SET question='$edited_question' WHERE questionID='{$_POST['question_num']}'") or die(mysql_error);
header("Location:admin_questions.php");
}
use jquery for that.it is easy and simple to use.
for depanding dropdown,jquery code is:
$(document).ready(function(e) {
$("#abc").change(function()
{
var firstfeild= $("#firstfeild").val();
$.post("getdata.php",{"firstfeild":firstfeild},function(data)
{
$("#abc").html(data);
});
});
});
and in getdata.php, just write mysql query.
function renderDropdown($res)
{
echo "
<select name='question_select' onchange=this.form.submit()>
<option value=null selected>--</option>";
while($row = mysql_fetch_array($res))
{
echo "<option value=\"".$row['questionID']."\">".$row['questionID']."</option>";
}
echo "</select>";
}
if(1==1)
{
echo "<form action='' method='post'>Edit If Question No.";
renderDropdown($res);
echo "</form>";
}
else
{
// $someOtherRes
echo "<form action='' method='post'>Edit Else Question No.";
renderDropdown($someOtherRes);
echo "</form>";
}
I've been having a rather irritating issue regarding capturing SQL information and then placing it into a PHP form (in theory, it should be kinda easy).
Here's the code for the SQL database information:
<?
$select = "SELECT * FROM beer WHERE country_id = 3";
$data = mysql_query($select) or die("Unable to connect to database.");
while($info = mysql_fetch_array($data)) {
echo '<center>';
echo '<h2>'.$info['name'].'</h2>';
echo '<table style="padding:0px;"><tr>';
echo '<tr><td><b>ABV%:</b></td><td width="570">'.$info['abv'].'</td></tr>';
echo '<tr><td><b>Bottle Size:</b></td><td width="570">'.$info['bottleSize'].'</td></tr>';
echo '<tr><td><b>Case Size:</b></td><td width="570">'.$info['caseSize'].'</td></tr>';
echo '<tr><td><b>Price:</b></td><td width="570">$'.$info['price'].'</td>';
echo '</tr></table>';
echo '</center>';
echo '<br/>';
echo '<img src="" border="0"><br><br>';
echo '<form name="cart" method="post" action="cart.php"> <table border="0"> <tr>';
echo '<td><input type="hidden" name="bname" value="'.$info['name'].'"><input type="hidden" name="price" value="'.$info['price'].'"></td>';
echo '<td><b>Quantity:</b></td>';
echo '<td><input type="text" name="qty" size="3"></td>';
echo '<td><input type="submit" value="Add to Cart" a href="cart.php?name=foo&price=bar" /a></td>';
echo '</tr></table></form>';
}
?>
I want when the submit value is pressed to somehow transmit the price, quantity and name to a basic HTML form (so that all the user has to do is add name, address, etcetc). I am completely stumped on how to do this.
If anyone could help, it would be much appreciated.
As you mentioned Amazon checkout, here is one thing you probably don't understand.
Amazoin doesn't use the form to move items data between server and browser to and fro.
It is stored in a session on a server time. All you need is some identifier put into hidden field.
To use a session in PHP you need only 2 things:
call session_start() function before any output to the browser on the each paghe where session needed.
Use `$_SESSION variable.
That's all.
Say, page1.php
<?
session_start();
$_SESSION['var'] = value;
and page2.php
<?
session_start();
echo $_SESSION['var'];
You wrote that code? because it's simply the same code as here.
You'll need to write an HTML form in your cart.php file
and use the $_POST variable to show the values of the price , quanitity and name.
For example:
<form method='post'>
<input type='text' name='price' value='<?=$_POST['price']?>'>
<input type='text' name='quanitity' value='<?=$_POST['qty']?>'>
I have two forms in the page one is filter, second is the item list, when I apply the filter to item list form, selected values reset to default. For filter I can save selected values, because I submit this form via POST method, but other remains not submited is possible to save selected that form values after page refresh? Here is some code of second form:
<form name="forma" method="post" onMouseOver="kaina();" onSubmit="return tikrinimas()" action="pagrindinis.php?page=generuoti.php">
<table width="540" border="1" align="center">
<tr>
<td>Client:</td>
<td>MB:</td>
<td>Price:</td>
</tr>
<tr>
<td>
<?php
$query="SELECT name,surname,pers_code FROM Clients";
mysql_query("SET NAMES 'UTF8'");
$result = mysql_query ($query);
echo "<select name=Clients id='clients'>";
echo "<OPTION value=''>- Choose -</OPTION>\n";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
echo "</select>";
?></td>
</tr>
</form>
You need to set the selected attribute of your select element based on what is in $_POST. Something like:
$selected = $_POST['Client'];
while($nt=mysql_fetch_array($result)){
if ($selected == $nt[pers_code]) {
echo "<option value=$nt[pers_code] selected="selected">$nt[name] $nt[surname]</option>";
}
else {
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
}
Also note that you should probably sanitize any values you get from $_POST.
Unfortunately there is no easy way to do this, and it's something that PHP and web developers in general have maligned for years, because repopulating form fields is never easy AND clean.
Values in the form you aren't submitting wont be sent (via GET or POST), so you're left with writing a custom workaround.
While it's a bit Q&D, I would recommend sending an Ajax call on form submit to store the values for your second form in the $_SESSION variable.
I'm sorry to say there's no easy alternative - due to the stateless nature of HTTP requests, this is something that every programmer has to struggle with.
just to break the question down to a more simplified form, let's assume we don't have the hassle of working with dropdowns. The real issue you're having is to take a value from form 1 and have it work even after form 2 has been submitted.
If you use a hidden field on form 2, of the same name as form 1, and populate it based on both form 1 and form 2's POST data, it will be "remembered"
<? // mostly html with a bit of php. ?>
<form id="f1" method="POST">
<input type ="text" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
<input type="submit">
</form>
<form id="f2" method="POST">
<input type="hidden" name="f1val" value="<?= htmlspecialchars( array_key_exists( 'f1val', $_POST )?$_POST['f1val']:'' ); ?>">
<input type ="text" name="f2val" value="<?= htmlspecialchars( array_key_exists( 'f2val', $_POST )?$_POST['f2val']:'' ); ?>">
<input type="submit">
</form>
<script type="text/javascript" src="js/jquery1.6.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#forma").submit(function(event) {
var 1stformfield = $form.find( 'input[name="misc"]' ).val();
/* stop form from submitting normally */
//event.preventDefault();
$.post("1stformsubmit.php", $("#forma").serialize());
//after it posts you can have it do whatever
//just post code here
$('#iframe1').attr('src', 'iframepage.php?client=' + 1stformfield);
window.frames["iframe1"].location.reload();
return false;
});
});
</script>
<form name="1stform" method="post" action="/">
<input type="text" name="misc" id="misc" />
<input type="submit" name="submit" id="submit" value="submit 1st form"/>
</form>
<iframe id="iframe1" src="" scrolling="no" ></iframe>
iframe page
<form name="forma" method="post" onmouseover="kaina();" action="/">
<table width="540" border="1" align="center">
<tr>
<td>Client:</td>
<td>MB:</td>
<td>Price:</td>
</tr>
<tr>
<td>
<?php
$selected = $_GET['Client'];
$query="SELECT name,surname,pers_code FROM Clients";
mysql_query("SET NAMES 'UTF8'");
$result = mysql_query ($query);
echo "<select name=Clients id='clients'>";
echo "<OPTION value=''>- Choose -</OPTION>\n";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[pers_code]>$nt[name] $nt[surname]</option>";
}
echo "</select>"; ?></td>
</tr>
</form>
This will post all inputs inside your form to the file you specify then you can have it do whatever you like, for example show a thank you message..Hope it helps.