So there's this hyperlink - it's happy being a hyperlink - it does not want to change to a button or a form element - it wants to stay a link!
But it would really help me if I could submit it via GET or POST (something which I switch on my pages due to design criteria). Is there ANY way that I can do this
thanks
Giles
You're in luck... clicking a hyperlink already does a GET request.
If you want to add query parameters, append them to the query string like so:
link text
Assuming you already have a form that you want to submit, you can use JavaScript to make the link submit the form:
<form id="myform">
...
</form>
Submit
As you do not want to go for form elements, there is no need of using POST method.
Simply, what you can do is:
Link
And in the page_location page,
<?php
$foo = $_GET['foo']; //here, assigns $foo = bar
// required actions
?>
Hope you get the solution! :)
I'll assume you're not really using the link as a link... that you want to submit a form with it. You'll want to keep the link inside the form you want to submit, and you can do this (jQuery):
$('#link').parents('form').attr('method', 'GET').submit();
or
$('#link').parents('form').attr('method', 'POST').submit();
Related
Here is a maybe simple question for you however complicated question for me. Since i am beginner I need to obtain and learn this quickly. I have asked similar question yesterday but it was not the thing that i wanted. I am wondering about is there a way to handle the tag on HTML handle the click event with PHP action and function.
On my 'Action.php' file I have a piece of code where the action is created:
$ARRAY_ACTION ["Frontend_upgradeChosen"] = array(
"VIEW" =>"upgrade/upgrade.view.php",
"VIEW_ERROR" => "error.php",
"TAB" => "UPGRADES",
"SUBTAB"=>"",
"AREA" => "GLOBAL",
"WITHOUT_CONNECTION" => TRUE
);
On the other hand here is my 'upgrade.view.php' where i am populating the a tag with list tag and inside the tags with the echo and for loop.
<form class="" role="form" method="POST" action='index.php?action=Frontend_upgradeChosen'>
<?
echo '<li><a href="index.php?action=Frontend_upgradeChosen" name="PRODUCT" >'."Click Me".'</a></li>';
?>
</form>
And finally here is my 'Controller.php' file and upgradeSearch function please forgive me but this question is crucial for me.
public function upgradeChosen (){
$chosenProduct = $_GET['PRODUCT'];
var_dump ($chosenProduct);
include $this->getPrintedView();
}
I can succesfully can populate tags, however i can not achieve the click event. I know it is possible with javascript but i do not want to do it in that manner i need to create an action as given the example above and i need to handle the click event of link tag. Please any help of suggestions on this I will be happy and you will save my life.
Thanks in advance
Here is the edited version lastly what i want to achieve is to when i can click on [Click Me] i want to fire the upgradeChosen Function and handle the clikc event of tag. the error that i am getting is
Notice: Undefined index: PRODUCT in ------- on line 1151
NULL
To get value from one page to another page in php, we should use GET or POST.
Question
When to use GET and POST
POST
To use post you have to submit the form.
<form method="post" action="abc.php">
<input name="username" type="text" />
</form>
To access the username,
$username = $_POST['username'];// this will give you what user has entered in the textbox.
Now you can also use GET as method in form.
$username = $_GET['username'];// this will give you what user has entered in the textbox.
Both the above methods are example for getting value on form submit.
Now if you want to get values on click of <a> tag, how to do??
use GET
Example
in php
$username = $_GET['username'];// this will give you had given in a tag
For multiple values in a tag,
in php
$username = $_GET['username'];// this will give you had given in a tag
$password = $_GET['password'];// this will give you had given in a tag
There is one more thing called REQUEST, which will have the values of both POST and GET
Read about
POST
GET
REQUEST
Can anyone help me .I need php code for checking whether a div is clicked or not.
like,
if (isset($_POST['Submit1'])) { }
But instead of submit button i need a div ...
anyone help me please
thanks
You will need JavaScript for this as PHP doesn't access the DOM if I'm reading your question correctly. I would recommend you add jQuery lib to your page as it's simpler to add click event to a DIV, otherwise you have to add an event listener in javascript yourself for click events on the DIV.
jQuery: http://api.jquery.com/click/
DIY: http://www.codingforums.com/archive/index.php/t-122993.html
I can't tell if you want a div to be treated as a form element, like a checkbox, or if you want to use a div to submit a form.
Using it as a form element has been explained by Stano, if you want the div to submit a form, you really should just use a submit button. If you want to use an image instead of a button, using <input type="image"...> will function as a submit button.
If you really need to use a div to submit the form, you will need javascript. Something like this, we take a form named "bald" and when you click the div, the form "bald" is submitted as if you pressed a submit button.
<form name="bald" action="somefile.php">
<div onclick="bald.submit();">Click here to submit</div>
</form>
PHP can't tell you if a div has been clicked as php only works on the Server. For this you need javascript.
jquery makes something like this very simple.
Can't quite imagine what can be this test good for, but you can use javascript like this:
<div id="clickable_div" onclick="document.getElementById('divclicked').value='YES'"> SOMETHING </div>
in your form
<input type="hidden" id="divclicked" name="divclicked" value="NO"/>
and in php
if ($_POST['divclicked']=='YES') {...} else {...}
I've never really thought about this, but it helps with some security of something I'm currently working on. Is it possible to submit GET data without an actual input field, and instead just getting it from the URL?
If so, how would I go about doing this? It kind of makes sense that it should be possible, but at the same time it makes no sense at all.
Perhaps I've been awake too long and need some rest. But I'd like to finish this project a bit more first, so any help you can offer would be appreciated. Thanks
Yes. If you add some query-string to yourl url, you can obtain that in php using $_GET without form submitting.
Going to this URL adress http://yoururl/test.php?foo=bar cause echoing foo (if there will be no foo query string, you'll get warning).
# test.php
echo $_GET['foo'] # => bar
Is this what you mean?
Link
// page.php
echo $_GET['type']; // foobar
This is what I understand of your question:
You have a <form method="get" action="foo.php">-like tag on your page
You have a series of <input type="text" name="bar"/> in your page
You want to pass additional GET parameters that are not based on an input from the form
If so, it is possible, but I hardly see how it could help with security. Input from a client cannot be trusted, so even if you hardcode the GET value, you have to check it serverside against SQL injection, HTML injection/XSS, and whatnot.
You have two ways:
Use a hidden input: <input type="hidden" name="myHiddenGetValue" value="foobar"/>
Add the GET parameter to the form action: <form method="get" action="foo.php?myHardcodedGetValue=foobar">
If what you meant is that you want to have a GET request without a form, you just need to pass all the GET parameters to the href of a link:
Click here!
Yes it's possible. Just append the GET data to the link.
For example:
<a href="main.htm?testGet=1&pageNo=54>Test</a>
You can also use Javascript to build the url.
If you happen to be using jQuery and want to build the GET data dynamically you can do this:
var getParams = { testGet:1, pageNo:54 };
$(".myLink").attr("href", url + "?" + $.param(getParams));
im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though
I have a form that submits a search for blogs on my site via GET.
The form works fine, but when submitted, the url on the following page reads ("Find it" is the value of my submit button):
www.example.com/search.php?sub_q=Find+it%21&q=tennis
This just isn't an aesthetically pleasing url. Is there a simple way to exclude the submit button from the URL? I would like the url to look like:
www.example.com/search.php?q=tennis
The search.php doesn't require $_GET['sub_q'] to operate, btw.
Try not naming your button (remove the name attribute) - it shouldn't be submitted if it doesn't have n name
See my answer here: SEO/PHP: How to Convert Form-Submit URL (Get-Method) without Javascript SEO-Friendly?
Basically: redirect to an easier-to-spell-out-URI