Retrieving values posted using the link - php

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

Related

send $_POST data via anchor tag

is it possible to somehow send $_POST[] data via a <a> tag? because it seems to automaticly execute the $_POST[] once the page loads rather than when I press the Anchor tag.
edit:
so what I'm trying to achieve is something like this:
I want an anchor tag which would normaly go to somepage.php?variable=something
but instead of using $_GET[] I want to send the variable via $_POST
Nothing in HTML will cause a link to trigger a POST request or encode data in the request body.
You can bind a JavaScript event handler to a link, cancel the default behaviour and then send a POST request by programmatically submitting a form or using XMLHttpRequest. This generally isn't a good idea and you should use a submit button instead (which you can style to look like a link if you really, really want to).
You can achieve this using jQuery and a HTML form
HTML:
<form method="post" name="redirect" class="redirect">
<input type="hidden" class="post" name="post" value="">
<input type="submit" style="display: none;">
</form>
Button: (html)
<a href='javascript:void(0)' class='button' var='DATAHERE'>sometexthere</a>
Javascript, or rather said jQuery:
$(".button").click(function() {
var link = $(this).attr('var');
$('.post').attr("value",link);
$('.redirect').submit();
});
this jQuery code listen's to any clicks on the items with the class button attached to them,
and reads out their "var" value, basicly you could use any kind of HTML element using this method as long as they have the button class attached to it.
Answer is no. What you can do is set the value of an input type when the <a> tag gets clicked. Using Javascript.
You could get hold of the query string from the href attribute of the anchor tag (you would need to parse the href and get hold of the string on the right hand side of the & symbol) and then post it using javascript or jquery (easier to use jquery).
http://api.jquery.com/jquery.post/
Would have to ask why you would want/need to do this?

Adding Google Analytics tracking code to contact form

I'd like to add Google event tracking to my PHP contact form. Doing so requires me to add a particular value to the 'onsubmit' attribute of the element. My PHP file already has an 'onsubmit' attribute defined. When I delete that attribute and enter the required Google code, nothing happens when I click the 'Submit' button (i.e. form does not submit, 'Thank You' page does not load, etc).
Here is the existing PHP code:
<form
class="cpp_form"
name="<?php echo $this->prefix; ?>_pform<?php echo '_'.$this->print_counter; ?>"
id="<?php echo $this->prefix; ?>_pform<?php echo '_'.$this->print_counter; ?>"
action="<?php $this->get_site_url(); ?>" method="post" enctype="multipart/form-data"
onsubmit="return <?php echo $this->prefix; ?>_pform_doValidate<?php echo '_'.$this->print_counter; ?>(this);">
Here is the Google code I need to enter for onsubmit:
_gaq.push([‘_trackEvent’, ‘button’, ’clicked’, ’contact us’,, ’true’])
Any ideas on how to do this/what I'm doing wrong?
I've also tried entering the Google code as the value for the "onclick" attribute. When I do that, the form can successfully be submitted, but it does not show up as an 'event' in Google Analytics.
Couple of things:
1) The GA code you posted shows smart-quotes. I'm not sure whether that's just a c/p thing posting here or if that's what you used in your actual code, but smart-quotes is invalid javascript syntax.
2) There are several ways you can do this. One way is to add it to your onsubmit as you tried. You don't need to replace the current stuff in there. Just add it to it. Since your current thing in there is returning something, you will want to add it before:
onsubmit="_gaq.push(['_trackEvent', 'button', 'clicked', 'contact us',,'true']); return <?php echo $this->prefix; ?>_pform_doValidate<?php echo '_'.$this->print_counter; ?>(this);">
Alternatively, you can look for where that xxx_pform_doValidate_xxx() function is defined and put your GA code in there.
3) To be clear, you are tracking that the form submit button was clicked. In my experience, tracking form submit button clicks has little value, since it does not indicate that the form was successfully submitted, nor does it tell you why submission failed if it failed.
You could add the google code to your validation function or use a separate function where you first put the google code and then return the value of your validation function. Or add it inline before the return statement...
But as you have a "Thank You" page, you could also track that instead although that will not give you the client- and server-side unvalidated submissions.

Hidden fields are not posted if the page gets submitted using "a href"?

<?php
$sort=$_REQUEST['sortby'];
echo" $sort ";
?>
<html>
<head>
</head>
<body>
<form method="get" id="thisForm" >
<table border=1>
<tr>
<td>
Day
</td>
<td>
Server
</td>
<td>
Service
</td>
<td>
Count
</td>
</tr>
</table>
<input type="hidden" name="sortby" id="sortby"/>
</form>
</body>
<script type="text/javascript" >
var sortArray=new Array("say","server","service","count");
function sorting( cnt)
{
alert(sortArray[cnt]);
document.getElementById("sortby").value=sortArray[cnt];
}
Based on the Header clicked i am trying to build a sort query but after it is submitted on clicking the "a href" link i am unable to get the hidden field posted data ?
I have changed "a href" to submit buttons then how do you stop the page being submitted if we click the same "sort by" link.
Here is what you're missing:
A link does not submit the form with input data, it just changes the url.
Submit the form.
Override the default <a> behavior (for example, using return false).
An updated version of sorting:
var sortArray=new Array("say","server","service","count");
function sorting(cnt)
{
document.getElementById("sortby").value = sortArray[cnt];
document.getElementById("thisForm").submit();
return false;
}
You can call it using:
Day
Working example: http://jsbin.com/ukoton/3
Another option is to use the popular library jQuery, which can greatly simplify your code:
$(function(){
$('#thisForm a').click(function(ev){
var sortValue = this.name;
// an alternative to this.name is to use a data-sortBy='day' attribute,
// and then $(this).data('sortBy')
$('#sortby').val(sortValue);
$('#thisForm').submit();
ev.preventDefault();
});
});
Working example: http://jsbin.com/ukoton/4
By default, anchor tags (that being A) do not submit a form. You can either use a button or submit the form using javascript.
You can submit the form from javascript with document.thisForm.submit()
Hmm .. I am not a php guy .. so I guess that initial 4 lines tries to get the sortby field from the form, anyways href link is not equivalent to html form submit, you will need to explicitly submit the form, maybe using javascript, to get the value
Just use a Submit button and style it to look like a link, if you need to. Using Javascript shouldn't be needed here.
Using an actual link instead of a Submit button will make the form unusable for people without javascript. That might include people using accessibility tools. Apart from that, it makes your page more complex (adding illogical HTML, and requiring Javascript), and therefore more complex to maintain.

Having Problems with a PHP Contact Form Inside DIV Set to Display:none by Default

We're using PHP to build a product page for a gallery website that's using GetSimple 3.0 CMS. We are trying to create a contact form that is displayed when you click a button. By default the contact form is in a DIV that's set to display: none. When you click the button it displays: block. When a user clicks the submit button for the form and calls the action it loads the contact.php file and resets the DIV to display: none resulting in the user not seeing the conformation text that their form was submitted. You can only see it by clicking on the contact button again and displaying that DIV to block manually.
We'd like the contact form DIV to persist after the submit button is clicked. I don't think showing our code would be helpful. We're just trying to find a way to implement this idea if possible.
At present our website is still early in it's development stage and it's still being hosted locally.
Thanks for any help.
use ajax submission of that particular form so that focus will not loss even the page will not be refreshed
If you can edit PHP code used for building the page, you can always display the style dynamically depending on the request, like this:
<div style="display:<?php echo (isset($_REQUEST['answer'])) ? 'block' : 'none';?>>
Thank you!
</div>
<form action="contact.php" method="post">
<input type="hidden" name="answer" />
...
</form>
You can also submit a form without reloading the page, e.g. with JQuery.Forms
If both the form and the PHP file are located in Contact.php, you could consider using this:
<div id="formWarp" <? if($_POST["contact"] == "true"){ echo 'style="display:none" '; } ?>>
<form method="POST" action="Contact.php">
<input type="text" name="something"/>
<input type="hidden" name="contact" value="true"/>
</form>
You can include any other <input>'s inside the form.
EDIT: Completely changed to reflect new information obtained to more directly address the problem.
Reading your above comments I'm fairly confident we are looking at the wrong function to edit. The wrapper element is not present in the function you posted which is what needs to have it's display toggled.
once you find that block of code we can edit it to look for the presence of post data which would indicate that the page is being loaded after a form submission has occurred (I can't promise that this won't cause it to trigger at the wrong time if there is more than one form on this page) and write in a style attribute to overwrite the default value if tested true.
Some where in that plugin there will be something defining the forms wrapper element
$html = '<div id="some-unique-id" class="some-class-name">';
You can break up this line and put a test for post data adding an inline style attribute if found
$html = '<div id="some-unique-id" class="some-class-name"'; //tag left open
if(isset($_POST) && (count($_POST))){ //Some Post Data Exists
$html .= ' style="display:block;"'; //Add display overwrite
}//Some Post Data Exists
$html .= '>'; //close the tag
The test we are performing here is if(isset($_POST) && (count($_POST))){ which is checking to make sure that A) $_POST exists and B) it has at least one element (this is using type juggling to convert a numeric result from count() into its Boolean equivalent (where anything greater than 0 will test true)
Now, as i mentioned, there may be more than one form on this page and it is possible it will be displayed afterwards which would auto show your contact form when you don't want to have it showing. Based on the example you provided in a comment it looks like this function exists within a class. If the block of code we are looking for exists within the same class it might be possible to leverage the id attribute to restrict the check to post data from the form you are interested.
$html = '<div id="some-unique-id" class="some-class-name"';
//Check for contact form specific post data
//(if $this->id is within scope and still the same)
if(isset($_POST) && (isset($_POST['p01-contact' . $this->id]))){
$html .= ' style="display:block;"'; //Add display overwrite
}//Post Data Exists
$html .= '>';
In this test we are hoping that $this exists and the attribute id of $this is the same as it was at the time that the form was originally drawn so we can look for post data that is related to the specific contact form. (the name we are looking for is based off of the code example you posted as a comment)
Unfortunately without looking at the source of the site/plugin it will be impossible for me to tell you where you can find what you are looking for. Once you find it though one of these two scenarios should hopefully take care of your problem.
Is this a publicly available plugin we could look at or something developed in-house?

How do you post data with a link

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

Categories