I'm new to web programming and I have a problem with the website I'm trying to do : I want to send some data to another .php page when the user clicks a link (there are several links, each link is the title of an newspaper article and when the user clicks one of them, it sends the id of the article because the next page has to load the content of the article). But I must not use Javascript.
Here is my previous code :
<h2> Articles </h2>
<?php
$articlesSql = "SELECT id, title FROM Article A, ApproveArticle VA WHERE A.id = VA.article AND VA.state='published';";
$articlesQuery = pg_query($connect, $articlesSql);
while($articlesResult = pg_fetch_array($articlesQuery)) {
echo "<form id=\"linkArticle\" action=\"article.php\">";
echo "<input type=\"hidden\" name=\"article\" value=\"$articlesResult[id]\"> </form>";
echo "<a href='#' onclick='document.getElementById(\"linkArticle\").submit()'> $articlesResult[title] </a> <br/>";
}
?>
Is there a way to avoid using Javascript? If so, how do I do?
Thank you for your help in this matter and have a nice day.
There is no way to submit a form without using a submit button with HTML only.
You can however make your button look like a link. This will require some CSS. Have a look at this thread for inspiration.
A better alternative that also solves your problem would be to ditch the form completely.
Simply include the article id you want to transfer to your PHP script into the URL as a parameter:
echo " $articlesResult[title] <br/>";
After the user clicked the link, you will be able to read the article id using $_GET['article'].
Related
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" ...
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.
I asked a similar question over here (http://stackoverflow.com/questions/13856915/contact-form-including-referral-of-previous-page).
"I have a php contact form using POST and I want to automatically include the referring page on the form, that is the website linking to the contact form. This would normally be within my website." I now have HTTP_REFERER working fine on my contact form to show the previous page link.
My next step is to have the same/similar result, but the referring page has a number of tours listed on the same page, so just including the URL isn't enough. I want to know which link/button they clicked to find the contact form.
Here is an example:
On tours.php I have the following code (multiple times on the same page):
<a href="<?php echo($domain); ?>/contact/booknow.php" </a>
I could change that to:
<a href="<?php echo($domain); ?>/contact/booknow.php?tourname" </a>
<a href="<?php echo($domain); ?>/contact/booknow.php?tourname2" </a>
On the contact form I want to store that they came from tours.php and tourname2, etc.
So how can I store that information on the contact form, so I know which button they clicked on (which tour they are interested in)?
Or do I need to add another variable in the link on the tour.php page?
Many thanks,
Andy
booknow.php?tourname1
booknow.php?tourname2
booknow.php?tourname3
When clicked, your next page will have access to $_GET['tourname1'] or $_GET['tourname2'] or $_GET['tourname3'] so you can test if they exists and you'll know on which link your visitor clicked.
I would suggest to change a bit for something like this :
booknow.php?tourname=1
booknow.php?tourname=2
booknow.php?tourname=3
So you can check if "tourname" exist and if so get its value "$_GET['tourname']".
Here you go :
$ref_url = $_SERVER['HTTP_REFERER'];
$ref_link = 0;
if( isset($_GET['tourname']) ) {
$ref_link = $_GET['tourname'];
}
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.
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