Post form in while loop with javascript submit - php

I have a while loop wich creates forms like:
$result3 = mysql_query("SELECT * FROM Afbeeldingen WHERE ImgId =$imgid ORDER BY AfbeeldingPrior DESC");
while($row3 = mysql_fetch_array($result3))
{
?>
<form method=POST name="form3" action="kamer.php">
<input type="hidden" name="id" value="<?php echo$kamerid;?>">
<input type="hidden" name="afbeeldingplus" value="12345">
</form>
<?php
}
I want to post these forms with a text link.
Normally I use
<script>
function f3_submit()
{
document.form3.submit();
}
</script>
and then I put echo "<a href=\"##\" onClick=\"f3_submit();\" >";
under the form
but because I got a lot of forms with the same name this wont work.
it doesn't post anything !
How can I post these forms with a text link, so without a submit button.

Remember to return false!
<script>
function submitForm(kamerid) {
document.forms["form"+kamerid].submit();
return false;
}
</script>
and have
echo '<a href="#" onClick="return submitForm(\''.$kamerid.'\');" >';

If I understand correctly, you want to have many different forms, and many links submitting different forms? If that's true, you have to name the forms differently, e.g. using the ID you get from DB, and then use this generated name in submit links.
Like:
<?php
$result3 = mysql_query("SELECT * FROM Afbeeldingen WHERE ImgId =$imgid ORDER BY AfbeeldingPrior DESC");
while($row3 = mysql_fetch_array($result3))
{
?>
<form method=POST name="form3_<?php echo $kamerid;?>" action="kamer.php">
<input type="hidden" name="id" value="<?php echo $kamerid;?>">
<input type="hidden" name="afbeeldingplus" value="12345">
</form>
<?php echo $kamerid;?>
<?php
}

I'm not sure that you're creating these forms in the way that you'd like to. But if you're happy with the way they are, then you can add in a submit button, and then just style it to look like a text link with css:
form input[type="submit"]{
background: none;
border: none;
color: blue;
text-decoration: underline;
cursor: pointer;
}

Maybe loop through the values and generate two comma (or pipe or whatever suits the task) separated strings and then create just one form and assign "id" and "afbeeldingplus" fields with these values. Your javascript submission will work fine and you have less HTML that having all these fields repeated either in one single form or across multiple forms.

while($row3 = mysql_fetch_array($result3))
{
<form method=POST name="form3" action="kamer.php">
<input type="hidden" name="id" value="<?php echo$kamerid;?>">
<input type="hidden" name="afbeeldingplus" value="12345">
<button type="submit">Text to Submit here</buttton>
</form>
}
and then just style the button to look like text

Related

Delete data from database using php and sql

So I have a php page that looks like this
<?php
echo "<table border='1' width= 300px >
<tr>
<th>Friend Names</th>
<th>Remove Friends</th>
</tr>";
while($row = mysqli_fetch_assoc($result2))
{
$friend_id_got = $row['friend_id2'];
$query3 = "SELECT profile_name
from friends
where friend_id = '$friend_id_got' ";
$result3 = $conn->query($query3);
$final3 = mysqli_fetch_assoc($result3);
echo "<tr>";
echo "<td>" . $final3['profile_name'] . "</td>";
echo "<td>"
?>
<form action="friendlist.php" method= "POST">
<button id="add-friend-btn" type= 'submit' name = 'submit'>Unfriend</button>
</form>
<?php
"</td>";
echo "</tr>";
}
echo "</table>";
When I press the button, I need the corresponding name to delete it. The only problem I'm facing is that How do I get the name corresponding to the button.
I think we need to relate the buttons and the name somehow, so when a specific button is pressed i get the corresponding name
From the question and comments, and a glance at your code it sounds like you probably actually need two pieces of data to be submitted to the server when your button is clicked:
The action which should be undertaken in response to the request (i.e. unfriending)
The ID of the person being unfriended
To achieve that you can add some hidden fields to your form. These are invisible to the user but will be available to PHP in the $_POST data when the form is submitted.
Something like this:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="unfriend_id" value="<?=$friend_id_got ?>" />
<input type="hidden" name="action" value="unfriend" />
<button id="add-friend-btn" type="submit" name= "submit">Unfriend</button>
</form>
Following on from the comment from #ADyson:
<form action="friendlist.php" method= "POST">
<input type="hidden" name="cancel_id" value="<?=$friend_id_got ?>" />
<button id="add-friend-btn" type="submit" name="submit">Unfriend</button>
</form>
By including a hidden field in the form, you're able to store more information.
You can see that I'm storing the ID of the friend you're unfriending in the value of the hidden field, so when the form is submitted (the button is clicked) you'll have access to "cancel_id" in the POST data, which will obviously contain the ID of the friend to unfriend.

Show forms from php with jquery in php

I have a php file that has 3 forms inside and some insert queries and i separate them with if(isset()) based on what variable comes from $_POST. I want to make the forms appear for example on the center of the page and when the user presses submit the form fades out and the next one from the isset appears. How can i do that?
For example this is the first form
<?php if (!isset($_POST['date'])): ?>
<?php if (!isset($_GET['id'])):?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
Date :
<input type="text" name="date" id="date"> </br>
<input type="submit" value="Submit" />
</form>
<script>
$("#date").datepicker();
</script>
<?php endif;?>
<?php endif;?>
And then if the user presses submit there is an if(isset()) with an insert query.
After the insert query there is another form
<?php if (isset($_GET['id'])): ?>
<form action="<?php echo $_SERVER['PHP_SELF']?><?php echo " ?id=" . $_GET['id'] . " &date=" . $_GET['date'] . " &seats=" . $_GET['seats']; ?>" method="post">
Ώρα :
<input type="text" name="time">
<input type="submit" name="submit">
</form>
<?php endif;?>
So what i want is to display them on the center of my page but i dont have to open another page every time i press submit and they will fadeout (or something similar) and display the next form.
Sounds like you could do this with css. Not sure I understand your question though.
display all 3 forms on the page at once -> no isset stuff, I don't get what that is there for
hide the ones you don't want the user to see with a class ".hide"
submit the forms with jquery, as you don't want the page to reload. after submitting, hide the one you just sumbmitted by setting its class to hide and removing hide from the next form
form{
transition: opacity 2s;
}
form.hide{
opacity:0;
}

When user searches in input box, redirect to a different page

I have an input box, which searches a database for the inputted text and displays the information on the page where the input box is.
Is it possible for once the user has typed in text in the input box and clicked submit, it redirects them to page.php and displays the results on page.php?
$getUsers = mysql_query("SELECT * FROM Users");
$numUsers = mysql_num_rows($getUsers);
$Online = mysql_query("SELECT * FROM Users WHERE $now < expireTime");
$Online = mysql_num_rows($Online);
echo"
<div class='members-browse-page'>
<div style='width: 876px; height: 28px; margin-bottom: 10px; clear: both;'>
<span class='form-label' style='margin-right: 30px;padding-top:5px;'>
<strong>Search:</strong>
</span>
<span>
<span class='search-bar'>
<form action='' method='POST' style='margin:0;padding:0;'>
<input type='text' name='UserSearchBar' maxlength='100' style='width:400px;'>
<input type='submit' name='UserSearchSubmit' value='Search Users'>
<a href='online.aspx'><b>Online (".$Online.")</b></a>
</form>
</span>
</span>
</div>
Simple Give action to your form
<form action='yourpage.php' method='POST' style='margin:0;padding:0;'>
<form action='page.php' method='POST' style='margin:0;padding:0;'>
Just write page.php in form action. you can get search box value on page.php using $_POST method. like $_POST['UserSearchBar']. Use this post value or write appropriate code for it and display result.

Removing the Name button in the address bar

When I try to do
Search in Form
i get in address bar the following entry
check.php?go_btn=&s=banana
Why do I get the name of the button "go_btn"
I should get the value
check.php?s=banana
Why this value
name = "go_btn"
Appears in the address bar
simple_search.html
<form action="check.php" method="get" id="my_form">
<input name="go_btn" type="submit" id="go_btn" form="my_form" formaction="check.php"
formmethod="get"
style="background-image: url(images/go.png); border: solid 0px #000000; width:
71px; height: 55px;" value="">
<input type="text" name="s" id="s">
</form>
check.php
<?php
if (isset($_GET['go_btn'])) {
$s_find = $_GET['s'];
if ($s_find == "banana") {
print ("you find banana");
}
else {
print ("blablabla....");
}
}
?>
You get it because you asked for it:
<input name="go_btn" [..snip...] value="" />
^^^^^^^^^^^
Any <input> field with a name attribute will get submitted along with the rest of the form. If you don't want go_btn being submitted, then take away the name attribute.
Well, In your PHP instead of asking about go_btn ask for s as follows:
<?php
if (isset($_GET['s'])) {
$s_find = $_GET['s'];
if ($s_find == "banana") {
print ("you find banana");
}
else{
print ("blablabla....");
}
}
?>
And then remove the attribute name from your form's element go_btn as follows:
<form action="check.php" method="get" id="my_form">
<input type="submit" id="go_btn" form="my_form" formaction="check.php"
...
You will get it because you asked for it. Your form method is GET and the GET method takes all values with it in the URL. If you would like not to see those values in the URL, you may use the POST method.
Moreover, you shouldn't be having ANY problem with that term in the URL, simple because you need that variable in the other file.

save PHP form data without submit on page refresh

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.

Categories