How can I convert this $_GET method to $_POST method?
$linkword .= "\n$alpha[$c] ";
You cannot pass $_POST data in a url, you must use cURL (PHP), AJAX (javascript), or a similar tool to build full HTTP requests.
ALTERNATE SOLUTION
You could however build a small form that submits, but you would have to use a submit button control for the "link" and use some hidden form inputs. You can Re-style the button anyway you wish with CSS.
<form action="link/url.php" method="post">
<input type="hidden" name="letters" value="value of letters" />
<input type="hidden" name="n" value="value of $n" />
<input type="submit" value="text of button" name="submit" />
</form>
You cannot POST data using URL Parameters. You will have to use a form with a method set to POST. Then submit the form on clicking the link or use a button.
Solution would be to use hidden form elements for your params and submit the hidden form when your click on the anchor tag.
Another solution would be to use ajax to post data. If you use jQuery or some libraries, you can do so.
Instead of creating a anchor tag. Create a hidden form
$linkword .= "\n$alpha[$c]";
Here is a Sample
<form name="hiddenform" method="POST" action="page.php">
<input type="hidden" name="letters" value="<? echo alpha[$c].$letters; ?>" />
<input type="hidden" name="n" value="<? echo $n; ?>" />
<a onclick="document.forms['hiddenform'].submit();">Test Link <? echo $alpha[$c]; ?></a>
</form>
Jsfiddle Demo
Related
I'll try and explain this as best as I can. Basically, I'm using a form to receive a comment. Upon hitting submit, the action creates a link similar to this: http://localhost:8080/camagru/comment.php?comment=test&post=Post
I have a variable with the image name in it that I want to pass as well, so something like this: http://localhost:8080/camagru/comment.php?img=test.png&comment=test&post=Post
I've tried using <form action="<?php echo commentpost.php?img=$img?>"> But everytime the submit button is pressed, it erases the img variable from POST and only puts in the new variables from the form.
Any suggestions?
add new hidden field in form tag like that
<form action="commentpost.php" method="post">
<input type="hidden" value="<?php echo $img ?>" name="img" />
<input type="submit" value="Save" name="IsSubmit" />
</form>
Now you can able to use $_POST['img']
The img variable is in GET.
If you want it in POST, try <input type="hidden" name="img" value="test.png">
use quotes in your case:
<form action="<?php echo "commentpost.php?img=$img"; ?>">
the best practice is to insert hidden element into your form:
<input name="img1" type="hidden" value="test.png" />
<input name="img2" type="hidden" value="test2.png" />
I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit
I am very new to php, I am having a problem passing data from textbox to a php variable to use it in an anchor tag. Below is the code i am using.
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
I want to pass value from searchbutton to anchor tag
Hello"
If you are using jquery in your project and want to do this on the front end, you can do:
<a id="someLink" href="http://www.example.com/?q=var" target="_blank">Hello"</a>
$('#someLink').attr('href', "http://www.example.com/?q=" + $('q').val());
With php you'd only be able to set the entered value of q on a post. (meaning when someone submits the form)
i.e.
Hello"
IF you need to populate the link href without a page refresh, you'll need to use javascript, if you want it to be populated after a form post, you can use php.
Be aware though that the link would need to be on the page set in your forms action attribute to populate the link
You should be aware that you take precautions when echoing out form submissions, however the level of questions suggests you've got more to learn before that. (No offense intended)
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
On your fetchvalues.php page
<?php
$val = $_GET['q'];
?>
then
Hello
You can do like this
<?php
if(isset($_REQUEST['searchbutton']))
{
?>
Hello
<?php
}
?>
I want to create a hidden field in my HTML page, all good, just a simple <input> field. Then, I'm trying to read the value of that input field as the page loads the first time, so the closest I've gotten to that is using a $_GET statement. I'm simply trying to echo out the value so that I can see that it's working like such:
<input type="hidden" name="selection" value="fixtures">
<?php
echo $_GET['selection']
and that just gives me nothing, if I try:
echo "<h1>$_GET['selection']</h1>
then I get 0.
Question is - what <form> parameters you have? Did you submit form? Simples example for it will be:
//form.php
<form method="GET" action="next.php">
<input type="hidden" name"selection" value="fixtures" />
<input type="submit" name="submit_btn" value="Send" />
</form>
//next.php
echo '<h1>'.$_GET['selection'].'</h1>';
You can achieve same thing with:
Link
I'm very interested in if it is possible to connect javascript variables to php. I know that in php we can write javascript code, but on the contrary we could not. To express my aim better , lets bring example like that:
<form name="some" action="<?php $_SERVER['php_self']; ?>" method="post">
<input type="submit" name="but" value="Action">
</form>
My question is how to make after pressing submit button to confirm (alert) with javascript and if it is confirmed do something (with php) and if isn't cancel (php operation).
You can do two things to pass javascript variable to php:
You can pass it as a hidden input field and submit it using POST
<input id="myHidden" name="myHidden" type="hidden"/>
Assign javascript variable to hidden input something like
var myVariable;
document.getElementById("myHidden").value = myVariable;
You can pass it as a query string with in your URL
As for confirming you can use javascript confirm, which will post on OK and not post/cancel on CANCEL
Something like:
<input type="submit" onclick="return confirm('Are you sure you want to submit?')"/>
<form name="some" action="<?php $_SERVER['php_self']; ?>" method="POST"
onsubmit="document.getElementById("response")
= confirm('some question') ? 'yes' : 'no';
return true;">
<input type="hidden" name="response" id="response" value="">
<input type="submit" name="but" value="Action">
</form>
BTW action="<?php $_SERVER['php_self']; ?>" opens up your page to XSS attacks.