How do you post data with a link - php

I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?
Regards

If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:
function formSubmit(house_number)
{
document.forms[0].house_number.value = house_number;
document.forms[0].submit();
}
Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:
<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">
<?php
foreach ($houses as $id => name)
{
echo "$name\n";
}
?>
</form>
That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavaScript submits the form.

I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:
<a href="house.php?id=<?php echo $house_id;?>">
<?php echo $house_name;?>
</a>
Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric() and then display its info.

You cannot make POST HTTP Requests by some_script
Just open your house.php, find in it where you have $house = $_POST['houseVar'] and change it to:
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
And in the streeview.php make links like that:
Or something else. I just don't know your files and what inside it.

This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.
#ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.
however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.
here is an example is i use:
in PHP Class,
public function styleButton($style,$text){
$html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
$html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
$html_str .= "<input id='view_button' type='submit' value='".$text."' >";
$html_str .= "</form>";
return $html_str;
}
Then in the CSS id="view_form" set "display:inline;"
and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"

I would just use a value in the querystring to pass the required information to the next page.

We should make everything easier for everyone because you can simply combine JS to PHP
Combining PHP and JS is pretty easy.
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";
Or a somewhat safer way
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";

This post was helpful for my project hence I thought of sharing my experience as well.
The essential thing to note is that the POST request is possible only with a form.
I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.
so I basically created each of the navigation items as a form using a loop as follows:
<ul>
begin loop...
<li>
<form action="/" method="post">
<input type="hidden" name="country" value="India"/>
<button type="submit" name="button">India</button>
</form>
</li>
end loop.
</ul>
what it did is to create a form with hidden input with a value assigned same as the text on the button.
So the end user will see only text from the button and when clicked, will send a post request to the server.
Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.
here is a screen shot of the navigation...
enter image description here

Related

Words after question mark are missing when I open a link in php/HTML

I know that it's kind of wrong to ask a question that's almost been answered in the past, but even though I'm trying to use this solution, it doesn't work.
Basically, I have a php table with a search link created in a HTML button from the strings inside the table.
Let's say that this string is creating a search in the website called Discogs and that the information I'm searching through a created link is the word "Beatles".
www.discogs.com/search/?q=Beatles
You notice the ?q=
Everytime I click on the link through a php code which looks like this, the opened link brings me to www.discogs.com/search/? WITHOUT THE REST OF STRINGS (in that case, the word Beatles)
I tried to rawurlencode the ? to have it as a %3F.
Here is what my code looks like
$discogslink = 'http://www.discogs.com/search/'.rawurlencode('?').'q='.'Beatles' ;
$form = "<form action='$discogslink' >";
$form .= "<input type='submit' value='Discogs'>";
$form .= "</form>";
echo '<td class="'.$lps->type.'">'.$form.' </td>';
The link that I would like to open is
http://www.discogs.com/search/?q=Beatles
The link that opens is :
http://www.discogs.com/search/? (nothing after the '?'...)
Do you have any idea why it does that?
BONUS QUESTION : How can I make the button open in a new tab instead of the same one?
The reason for this is the default behaviour of HTML forms. They send data as a GET request, just like links do. So in your case the content of the form (which is empty) overwrites your q-parameter because the form content wins over the parameters specified in the action link.
The solution is to add a hidden field:
<input type="hidden" name="q" value="Beatles"></input>
And concerning the new window:
<form target="_blank" ...

i have a textbox which extract the content of dropdownlist now whenever i change the extracted content it should be saved into database

I have a text box which extracts the content of dropdownlist.Now whenever i extract the content i too need to edit it ana save it into the database.How can i do????
Here is my code:
<?php
require'conn.php';
$select_query="Select dynamictext from tbl_content where type=1";
$select_query_run =mysql_query($select_query);
echo'Dynamictext:';
echo "<select name='dynamic text' id='names' >";
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
$value=$select_query_array["dynamictext"];
echo "<option value='$value' >".htmlspecialchars($select_query_array["dynamictext"])."</option>";
}
echo "</select>";
?>
Based on the clarification I got above from #krisha above, I'm going to take a stab at answering this. You'll want to refer to my comment above, for a definition of (Option A) and (Option B), as I defined them.
Let's assume you've got (Option A) working and that (as far as the select HTML element is concerned), it is functional.
Let's also assume that you know to do the following:
Place the select tag inside of a form tag.
Set the form tag's action and method values.
Place a <input type="submit" value="Submit"> inside of the form tag.
If none of the above made sense, see here.
Once you've done everything above, that will result in the value of the HTML drop-down being available to PHP after the user clicks the Submit button and the page refreshes. How the value of the drop-down is passed through the submit process will depend on the method value you pass to the form tag. I'll assume you use method="get" (which will result in the value of the drop-down appearing in the URL after the refresh). If you want more info on get versus post, see here.
Once the refresh occurs, you use PHP's $_GET[""] to retrieve the value of the HTML drop-down. In your case, you would use $_GET["dynamic text"] (since the name of your select is dynamic text). You could set this value to a variable, like so:
$value_of_select = $_GET["dynamic text"];
At this point, you have the value the user selected from the HTML drop-down. Now, push it to the database. It looks like you already understand how to pass queries to a database. The only difference in this case is that you want to do an insert or an update, not a select.

I have a short html form with a php for loop which allows the user to build the same form up to ten times on the page

The $i variable is each field and is populated in the 'inp' boxes which are just input boxes and sboxes which are just select boxes. There is only one form when the page loads and it has all the criteria for a trainer to be added. The trainer name would be trainer_name1 on the first form. If they chose to hit the new button they could fill out the information for another trainer, the input box for the second form for 'name' would just be trainer_name2 and all the other fields are named respectively to what they are in the form. As new forms are built in just adds the next consecutive number onto the end of whatever the field might be named.
Here is my code:
<fieldset><legend>Trainer Request</legend></fieldset>
<tr><td><input type='button' onClick="if (show_item(1,10, 0)) { this.style.display = 'none'; }" value='New'></td></tr>
<?php
$contact_array = array('ACCEPTED TRAINING','DECLINED TRAINING','LEFT MESSAGE FOR TRAINING ACCEPTANCE',
'NEED TO CONTACT TO SEE IF INTERESTED',
'NEED PAPERWORK/TRAINING',
'NEED SIGNED CONTRACT AND PAPERWORK',
'NEED TO COMPLETE TRAINING');
for ($i = 10; $i > 0; $i=$i-1)
{
echo "<table id='hidden$i' style='display:none;'><tr>";
echo "<td>Date</td><td>Status</td></tr>";
echo "<tr><td>"; inp("date$i"); echo "</td><td>";
sbox("contact$i", $contact_array, 0, 'wide2');
echo "</td></tr>
<tr><td>Facility</td><td>";
inp("facility$i",50); echo "</td></tr>";
echo "<tr><td>Trainer Name</td><td>";
inp("trainer_name$i",35);
echo "<tr><td>Distance From</td><td>";
sbox("distance_from$i", array('1','2','5','10','15','20','25','30','40','50','60','70','80','90','100'));
echo "</td></tr>
<tr><td>Phone</td><td>";
inp("phone$i",13,'phone');
echo "</td><tr><tr><td>Email</td><td>";
inp("email$i",50);
echo "</tr><tr><td>Address 1</td><td>";
inp("addr1$i",50);
echo "</tr><tr><td>Address 2</td><td>";
inp("addr2$i",10);
echo " City ";
inp("city$i",20);
echo "</td></tr><tr><td>State</td><td>";
inp("state$i",2);
echo " Zip ";
inp("zip$i",'zip');
echo "</td></tr><tr><td>Notes</td><td>";
tbox("notes$i", 40, 3);
echo "</td></tr></table>";
}
?>
<script type='text/javascript'>
show_item(1,10,1);
</script>
As you can see down here I'm building a link which would name the link whatever the trainer name is, in this case trainer_name1 is Tim Jackson, so i've just built a hyperlink with his name.
<?php
// print_r ($_GET);
echo sendback_link($_GET['trainer_name1'], 'ACS/TrainerLookup', 'trainer_id=trainer_code&trainer_name=trainer_name');
?>
I'm confused on how to add a dynamic link like this into the for loop so as the form builds 1 - 10 each trainer_name2, trainer_name3, trainer_name4 etc etc. will have their names hyperlinked.
I'm thinking I create a new variable for the number 1-10 and append it onto the $_GET[trainer_name$].. something like that?
I hope that makes sense and any help would be greatly appreciated.
If I understood your question correctly, you are trying to add more content or replace content on a webpage that has already been generated with PHP.
I think what you are trying to do can be achieved using AJAX.
AJAX is a technique for creating dynamic webpages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
In your case, the process can be described in 3 steps:
the browser making a new request to your server (using javascript)
the server processes the request and sends some information back to the browser
the browser deals with that information (again using javascript) and then updates part of the webpage
There is an example here that shows how you can dynamically change the webpage contents.
Although, if you are allowed to, I would suggest that you use a javascript framework like jQuery which simplifies the whole process (google it for download and instructions on how to use).
You can read about using AJAX with jQuery here and reading the examples that follow to better understand how you can use it.
If you are going to try and use AJAX with jQuery or simply AJAX I suggest that you try it on a test page to get a simple working example, then adding a few things and checking to see if everything works as expected. When the test page is working as expected, import the code to your page.
This is how I would do it, there may be better or easier ways to do it.

Retrieving values posted using the link

I am trying to submit a form using a hyperlink and it is not posting values onto the next page.Here is my code form:
<?php
$email = array('name'=>'accountno','id'=>'accountno','value'=>set_value('email'));
?>
<form method="post" id = "login_form" action="/salesrep/check" name = "login_form" class="custLogin">
<fieldset style="color: #BD1313; width: 440px;"> <input type="hidden" name="submit_type" id="submit_type" value="account_only">
<br><center><label for="customerNo"><b>Customer No:</b></label>
<? echo form_input($email);?>
Submit<? echo form_input($button);?> </center>
<p> </p>
</fieldset>
</form>
The code on the next page looks like this:
<?
print_array($_POST);
die();
?>
When i use the button here,it posts values to next page successfully.BUT I HAVE not been able to post values using the hyperlink on onclick event. Where i am making mistake??
Why i am getting empty array when i am already inserting value in the text box.?? Or is there any way i could post values using the link and retrieve them in the next page???
The real problem is that you are trying to use a link plus some JavaScript to submit a form in the first place. Don't do this. Buttons inform users that they will submit the form. They will show up in screen readers when they are in forms mode (a link with some JavaScript won't). They "just work".
If you insist on using a link and some JavaScript, then the reason that your code doesn't work is that the JavaScript runs, the form starts to submit, then the link is followed and a GET request is made to the page instead.
Normally you could call preventDefault on the event to stop this, but you are using old style intrinsic event attributes so you need to return false; from there instead.
Recommended reading: Progressive Enhancement and Unobtrusive JavaScript

ID Getting Lost In Loop

I've been working on a way to build an archive for new threads. The over all goal was to make it so that if someone wanted to edit or delete a news thread they could, as well they could save a thread as a draft so that it ain't displayed to the public. I am using MySQL to store all the news threads, and I have it so that it prints out every news feed and the information for it. But when i click the edit button to edit that thread, it ALWAYS uses the id for the last MySQL entry called and NOT the ID I set it to use via a hidden form. Anyways here's the code and all parts to it. I'm so confused, and could really use some help. If you got questions just ask.
Main Script: http://pastebin.com/hn3cgVXu
Article_Post: http://pastebin.com/hhaLkuXe
Article_Archive: http://pastebin.com/X2fDg4dk
The original value for ID is called from the database, and set from article_archive
Display:
http://i25.photobucket.com/albums/c51/dog199200/Untitled-2.png
The Pencil is Edit, Trash Can is Delete. The image clearly shows that the loop is getting the ID, but that specific ID isn't being passed when the edit image is clicked.
In your Article_Archive when you loop through your database results you are naming your hidden input field the same thing for all the results.
<?php
while($row = mysql_fetch_array($news_list)) {
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" id=\"result_".$row['id']."\" name=\"result_".$row['id']."\">";
// ...
echo "... <input type=\"hidden\" name=\"id\" value=\"".$row['id']."\">";
// ...
echo "</form>";
} ?>
You're calling it id, so when you place multiple hidden input fields on the same form it will just grab the last one. Where is the javascript for when you click edit? You won't be able to do a standard form submit with that code since you're overwriting all the input fields with the same name attribute.

Categories