I don't know if what I want to do is possible (or too complicated).
I have a form that inserts the data into my DB. In it I have a select. I want to hide or remove the options that are already in my DB (so the user can't see them).
Every user has his own .php page where I edit the form with new options after the old ones are used.
PHP (In the same page, above the form):
<?php
if ($registro != "") {
include("conexao.php");
$conexao = #mysql_connect($host,$user,$pass)
or die("Cadastro");
$query = "INSERT INTO certificados VALUES ('$id','$registro','$name', now() )";
mysql_select_db($db);
mysql_query($query,$conexao);
}
else {
?>
HTML:
<form name="inserir" action="<? echo $PHP_SELF; ?>" method="post">
<select name="registro" id="registro">
<option value="">Select one</option>
<option value="11891">11891</option>
<option value="11892">11892</option>
<option value="11893">11893</option>
<option value="11894">11894</option>
<option value="11895">11895</option>
</select>
<input type="text" name="name" placeholder="Name">
<button type="submit" id="submit" name="B3">Send</button>
</form>
Edit 1: I'll try to explain better what I want to do.
Imagine that the user selected the first option value "11891" and submitted the form (thus inserting this value in the DB). After the page reloads, this value is no longer listed for him in the select. So based on the form that I posted here, now he would only have the options "11892", "11893", "11894" and "11895".
PS: Sorry for any mistakes or use of outdated parameters. My father wrote this code (he has some knowledge of php). In my case, my knowledge is almost nonexistent.
Thank you all for the help.
You should have 2 more tables. One 'tb_options' where you retrieve all the options. Another one 'tb_users_options' where you store all option based on user ID. So when retrieving the options from tb_options you select only those not present in tb_users_options Taking into account the user ID. This is a logic matter.
Related
I have searched many solutions and almost all over the internet but found no answer.
I am having a form like:
<form action="index.php" method="get">
<select id="store" type="text" name="s">
<option valA="value1" valB="value2">Selection 1</option>
<option valA="value3" valB="value4">Selection 2</option>
</select>
<input id="submit" type="submit" value="Submit" />
</form>
This form is extended via PHP with MySQL which takes valA and valB from database for each "Selection" in while loop and generate form with that form (given above).
I know that sending it via POST will reload my website with value given by value name. But I need to do it with two values.
So my link when I select one of the options will be:
http://mywebsite.com/index.php?valA=value1&valB=value2
Form is still active every time website reloads, so when I choose another option, I want to change it with submit and change URL.
Everything I found is to use multiple option and send two of them at the same time, but I have no multiple selection, but one selection with many parameters each (for me - two).
Is it possible to do it via HTML/Form, or I need to use PHP, but how?
I need to reload website - not change it only, because based on valA and valB website will load datatables script based on it and will produce table from database one of the values is just a key to table, another to database.
Maybe you can join your required values with some delimiter, for example:
<select id="store" type="text" name="s">
<option value="value1:value2">Selection 1</option>
<option value="value3:value4">Selection 2</option>
</select>
Your url becomes:
http://mywebsite.com/index.php?s=value1:value2
On server side you can:
$parts = explode(':', $_GET['s']);
You will need javascript to achieve that without php
If your php is whats receiving the call then why don't you have a value seperated by a delmiter value="value1|value2"
then in your php I would do the following
$values = explode("|",$_GET['s']);
then you have $values[0] and $values[1] - containing the two values
I want to make a drop-down list, each selection being a new URL link, where there is no Submit button. Instead the user goes straight to the URL when they select from the drop-down. I can make this work, using the following HTML:
<select name="mydropdown" class="class1" onChange="document.location = this.value" value="GO">
<option value="page1.php">Page1</option>
<option value="page2.php">Page2</option>
<option value="page3.php">Page3</option>
</select>
However, I also want to post two hidden PHP variables to the selected page, $username and $password.
Any help much appreciated.
You can try this by submitting the form by jquery. Also pass the password in encoded format. Check below code ==>
function callredirect(pagename){
$("#frmdropdown").attr("action",pagename);
$("#frmdropdown").submit();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" id="frmdropdown">
<input type="hidden" name="username" value="abc" />
<input type="hidden" name="password" value="password" />
<select name="mydropdown" class="class1" onChange="callredirect(this.value)" >
<option value="page1.php">Page1</option>
<option value="page2.php">Page2</option>
<option value="page3.php">Page3</option>
</select>
</form>
The best way to pass variables to other page is using the $_SESSION, since you don't have form and user doesn't want to know what variables/ values you are passing. read the docs here about PHP Sessions
NOTE: Advise not to pass password through out the page, it make your
system vulnerable for attackers. Best practice is verify user in each
page if you want or encrypt and store the password.
I'm posting this question up here because i'm a bit lost. This is my first time working on PHP and MySQL and I'm doing this for my uni assignment. I've managed to do all the CSS, Javascript and part of the PHP/MySQL requirements. The only thing that I have left is:
How do I get the value from my form that is generated through PHP and MYSQLquery and update the respective row of my MySQL table. I've attached an image to make it easier to understand.
The left side allows a user to search or display all orders. The PHP within will then call itself to update the table on the right. That part I managed to do it. But now part of my assignment requirement is:
The Manager should be able to ‘update’ the status of an order from a link
or button next to the order in the table, changing the status from
(pending | fulfilled | paid | archived). This could be done by returning
the record to a form in this webpage, to change and ‘confirm’ the change.
Now the table generated is actually a form. I've put some snippets of the codes here
echo "<form method=\"post\" id=\"regForm\" action=\"https://formtest.php\" name=\"editAll\">\n";
.......
echo "<td>", $row["order_status"],"</td>\n";
echo "<td><select name=\"change_orderstatus[]\" id=\"change_orderstatus[]\">
<option value=\"\" selected=\"selected\" >Please Select</option>
<option value=\"pending\">Pending</option>
<option value=\"Fulfilled\">Fulfilled</option>
<option value=\"Paid\">Paid</option>
<option value=\"Archived\">Archived</option>
<option value=\"Delete\">Delete</option>
</select></td>\n";
....
echo "<input type= \"submit\" value=\"Save changes\" id=\"commit\" name=\"editAllquery\" />";
If I put change_orderstatus[] it supposedly becomes an array but how do I make use of this to update my MySQL Table
I'm attaching another picture to show the bottom where I have the save changes button
Now I know that Stackoverflow is known for bashing people instead of helping because "all the noobs should learn their stuff and all" (this is from my personal experience) but I'm not asking you to write the codes for me or help me do my assignment.
I'm just asking you to point me to a direction where I can then do it on my own. That's all. I've tried to make my post as clear and as detailed as possible so that I don't get immediately down voted to oblivion.
If you could help me out, that would be great.
Thank you
First you'll need to output a form somewhere, I suggest making it like:
<form action="phphandler.php" id="form1" method="POST" style="display: none;">
<input type="hidden" name="id" id="inp1">
<input type="hidden" name="status" id="inp2">
</form>
Make onChange event for select :
<select onchange="event.preventDefault(); myFunction(1)" id="sel1">
myFunction(1) should be generated for each select with its unique id using php
Then function like:
function myFunction(id) {
document.getElementById('inp1').value = id;
document.getElementById('inp2').value = document.getElementById('sel1').value;
document.getElementById('form1').submit()
}
All ID's are generated by php except for inp1 and inp2 and form1
I've been looking around on SO and I don't -think- this is something that's up here, either that or it's something I've missed. If so, please point me there and I'll simply mark this question as answered.
I have a completely working PHP form where my users input information into input boxes and there's a button at the bottom that submits the information to the MySQL database, so that part works perfectly. What I'm trying to do now is this: have 2 drop down menu's, each one with a static list of choices and I'd like those choices to also get sent to the database.
For instance, currently in my form I have First Name (frame) and Last Name (lname) that the user can input and then if I query the database it spits out First Name and Last Name perfectly. So now I'd like to add to my form a drop down box where the user can pick, for example, Boy or Girl, and then after doing that click the submit button that's already there (I don't want the drop-down to submit the data, and I don't want the drop-down to be populated from the database.)
I'm guessing I need to use Javascript for this part? But I really don't know.
Any advice would be appreciated.
<html>
<head>
<title>MyForm</title>
</head>
<body>
<form id="form" name="form" action="" method="post">
<Label>First Name/Organization</label>
<input type="text" name="firstname" value="<?php echo $firstname; ?>"/>
<input type="submit" name="submit" value="Add entry">
</form>
</body>
</html>
So, as part of the same form, you simply treat it just like the input boxes. Here's an example:
If the dropdown is named:
<select name="gender">
<option value="boy">Boy</option>
<option value="girl">Girl</option>
</select>
Then in your php, you would simply get the value for gender:
$gender = $_POST["gender"];
And add to your SQL statement where you are saving the first/last name the additional field and value for gender.
Of course, you would have to first modify the table to have the column for gender....
Hope that helps!
It sound s like you just want a simple html drop down as part if your form. Just use
<select name = 'gender'>
<option value = 'boy'> Boy </option>
< option value = 'girl'> Girl </option>
</select>
Then access $_POST['gender'] in the receiving script to put it into the database (after validation of course)
I have this simple Select box that is suppose to store the selected value in a hidden input, which can then be used for POST (I am doing it this way to use data from disabled drop down menus)
<body>
<?php
$Z = $_POST[hdn];
?>
<form id="form1" name="form1" method="post" action="test.php">
<select name="whatever" id="whatever" onchange="document.getElementById('hdn').value = this.value">
<option value="1">1Value</option>
<option value="2">2Value</option>
<option value="3">3Value</option>
<option value="4">4Value</option>
</select>
<input type="hidden" name ='hdn' id="hdn" />
<input type="submit" id='submit' />
<?php
echo "<p>".$Z."</p>";
?>
</form>
</body>
The echo call works for the last 3 options (2,3,4) but if I select the first one it doesnt output anything, and even if i change first one it still doesnt output anything.
Can someone explain to me whats going on, I think it might be a syntax issue.
EDIT: As mentioned in first paragraph: I am using the hidden field instead of just using the value of selected because i plan on disabling the select drop down in my actual website, and when you disable the menu the POST data is no longer accessible. By putting it into a hidden field you can still access the information
The first one is the default, so when you "change" to the first one, it hasn't actually changed and the event does not fire.
You should be reading the value directly from the select and not depending on JS to set a hidden field though. The JS is just pointless, unreliable complexity.
Does anything else in your client-side application depend on that hidden field? If the ONLY purpose of the hidden field is to duplicate the selected value of the dropdown, then there's absolutely no reason to use an onchange handler.
Best solution would be to set an onsubmit handler on the form itself to copy the dropdown value into the hidden field.
<form ... onsubmit="document.getElementById('hdn').value = document.getElementById('whatever').value">
Try your post access like this:
<?php
if (array_key_exists('hdn', $_POST)) {
$Z = $_POST['hdn'];
}
else {
$Z = 1;
}
?>
change your input:
<input type="hidden" name='hdn' id="hdn" value= <?php echo "$Z"; ?> />
this.value has no value. That is why $_POST['hdn'] doesn't have a value when you initially load the form.
As #David said, if you use Firefox you can see the post data for hdn is empty/null.