I have a url which is showing two values posted using a GET method in php.
I can get the values from the url using this example -
suppose the url is http://example.com/?name=Hannes
My code
<?php
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
?>
And the output Hello Hannes!
My question is- how can I set the values to the url on button click method post
eg.
<form method="POST">
if(isset($_POST['$btn'){
htmlspecialchars($_GET["name"])="Lucky"; /*If there is something like this*/
}
</form>
Thank you.
Try with -
if(isset($_POST['$btn'){
header('location:yourpage.php?name='.$yourValue);
exit;
}
Related
I cant figure out why this very simple test of using the input from the form doesnt work..
<?php
echo "TEST";
echo "<pre>" . print_r($_POST, true) . "</pre>";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
echo "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
When I display the array it shows that it is empty. When I enter something in the input field and click submit, nothing changes.
The demo page
I would be very grateful if anyone has an idea, thanks.
2 things
SubmitButton needs a value="something" then change
echo "<pre>" . print_r($_POST, true) . "</pre>";
to
echo "<pre>" . print_r($_POST) . "</pre>";
You only need the "true" if you want to return that as a var, like
$blob = print_r($_POST, true);
echo "<pre>" . $blob . "</pre>";
If anyone has a similar problem in future (I don't think this is the solution for this specific case):
I was debugging an application and php api since the past 4 hours, printing my output everywhere but I couldn't figure out what the issue was.
The production system was working fine while our testing environment made was not accepting any of my post arrays.
In the end it turned out, that I had a wrong url opened from the application (http:// instead of https://), so a .htaccess file redirected all my requests to https but removed all post information.
Hope this helps anyone.
Case :
Hi all, actually i use POST and GET method in many times and they're work fine. But this time it isn't. I use POST TYPE form and then do PHP While (Looping) to generate some HTML attribute, i also give name to these HTML attribute (like input field etc). But second script (different page name), just won't detect it. It's like im using 1.php (where POST method applied) and 2.php (where GET method applied).
Experiment :
My experiment with code is like this basically (just to show logic, not the actual code).
1.php
<?php
echo <form method="POST" action="2.php">
while (fetch) {
echo fetch
}
echo </form>
?>
2.php
<?php
$x = $GET['id'] // work fine
$x = $GET['html_attribute'] // result : unidentified index
$query = update tbl_transaction set x='$x' where id='$id'
$query -> execute();
?>
Real Code of 2.php
<?php
include 'config/user_session.php';
include 'config/config.php';
if(isset($_GET['id']))
{
$id=$_GET['id'];
$no_ref=$_GET['no_ref'];
$desc=$_GET['desc'];
$amount=$_GET['amount'];
$via=$_GET['slPayment'];
$date=$_GET['date'];
$cat=$_GET['cat'];
$subcat=$_GET['subcat'];
$query1=mysql_query("update tbl_transaksi set no_ref='$no_ref', desc='$desc', amount='$amount', via='$via', date='$date', cat='$cat', subcat='$cat' where id='$id'");
if($query1)
{
echo '<script language="javascript">';
echo 'alert("Transaction Edited.")';
echo '</script>';
echo "<script>setTimeout(\"location.href = 'edit_transaction.php';\",100 </script>";
}
}
?>
I tried to change GET with POST, the result still same. Unidentified Index.
Desired Output :
I want 2.php GET Function return value from 1.php attribute value. Form method already POST.
Where did i do wrong ? What code i should modify ?
Thank you Stackoverflow Community.
i'm trying to properly validate external url from a codeigniter 3 view.
I have in my db an url like this :
http://www.example.com/content.php?id=test&article=3
and i want to make a link like this one ->
http://www.example.com/content.php?id=test&article=3
I tried this :
<?php
if (isset($c_url_redir)){
$c_url_redir = 'http://www.example.com/content.php?id=test&article=3';
echo anchor(htmlspecialchars($c_url_redir), "go", 'class="pure-button pure-button-primary" target="_blank"');
}
?>
but i have the same url as the first one.
Could you help me?
Try using url_encode() on just the get variables. Personally, I'd have the first part of the URL assigned to a variable then tack on the url_encoded get variables e.g.
$c_url_redir = 'http://www.example.com/content.php';
$get_variables = url_encode('id=test&article=3');
echo anchor($c_url_redir . '?' . $get_variables, "go", 'class="pure-button pure-button-primary" target="_blank"');
dear all,
I need to send parameters to a URL without using form in php and get value from that page.We can easily send parameters using form like this:
<html>
<form action="http://..../abc.php" method="get">
<input name="id" type="text" />
<input name="name" type="text"/>
<input type="submit" value="press" />
</form>
</html>
But i already have value like this
<?php
$id="123";
$name="blahblah";
?>
Now i need to send values to http://..../abc.php without using form.when the 2 value send to abc.php link then it's show a value OK.Now i have to collect the "OK" msg from abc.php and print on my current page.
i need to auto execute the code.when user enter into the page those value automatically send to a the url.So i can't use form or href. because form and href need extra one click.
Is their any kind heart who can help me to solve this issue?
You can pass values via GET using a hyperlink:
<a href='abc.php?id=123&name=blahblah' />
print_r($_GET) would then give you the values, or you can use $_GET['id'] etc in abc.php
Other approaches, depending on your needs, include using AJAX to POST/GET the request asynchronously, or using include/require to pull in abc.php if it only includes specific functioanlity.eg:
$id="123";
$name="blahblah";
require('abc.php');
You can do:
$id="123";
$name="blahblah";
echo "<a href = 'http://foo.com/abc.php?id=$id&name=$name'> link </a>";
<?php
$base = 'http://example.com/abc.php';
$id="123";
$name="blahblah";
$data = array(
'id' => $id,
'name' => $name,
);
$url = $base . '?' . http_build_query($data);
header("Location: $url");
exit;
I'm new to forms and post data ... so I don't know how solve this problem!
I've a php page (page1) with a simple form:
<form method="post" action="/page2.php">
<input type="search" value="E-Mail Address" size="30" name="email" />
<input type="submit" value="Find E-Mail" />
</form>
How you can notice ... this form post the 'email' value to the page2. In the page2 there is a small script that lookup in a database to check if the email address exist.
$email = $_POST['email'];
$resut = mysql_query("SELECT * FROM table WHERE email = $email");
.
.
.
/* do something */
.
.
.
if($result){
//post back yes
}
else{
//post back no
}
I don't know how make the post back in php! And how can I do to the post back data are read from a javascript method that shows an alert reporting the result of the search?
This is only an example of what I'm trying to do, because my page2 make some other actions before the post back.
When I click on the submit button, I'm trying to animate a spinning indicator ... this is the reason that I need to post back to a javascript method! Because the javascript function should stop the animation and pop up the alert with the result of the search!
Very thanks in advance!
I suggest you read up on AJAX.
Here's a PHP example on W3Schools that details an AJAX hit.
Hi i think you can handle it in two ways.
First one is to submit the form, save the data in your session, check the email, redirect
back to your form and display the results and data from session.
Like
session_start();
// store email in session to show it on form after validation
$_SESSION['email'] = $_POST['email'];
// put your result in your session
if ($results) {
$_SESSION['result'] = 'fine';
header(Location: 'yourform.php'); // redirect to your form
}
Now put some php code in your form:
<?php
session_start();
// check if result is fine, if yes do something..
if ($_SESSION['result'] == 'fine) {
echo 'Email is fine..';
} else {
echo 'Wrong Email..';
}
?>
More infos : Sessions & Forms
And in put the email value back in the form field
<input type="search"
value="<?php echo $_SESSION['email']; ?>"
size="30"
name="email" />
Please excuse my english, it is horrible i know ;)
And the other one the ajax thing some answers before mine !
As a sidenote, you definitly should escape your data before using it in an SQL request, to avoid SQL injection
As you are using mysql_* functions, this would be done with one of those :
mysql_escape_string
or mysql_real_escape_string
You would not be able to post in this situation as it is from the server to the client. For more information about POST have a look at this article.
To answer your question you would want to do something like this when you have done your query:
if(mysql_num_rows($result)){ //implies not 0
$data = mysql_fetch_array($result);
print_r($data);
}
else{
//no results found
echo "no results were found";
}
The print_r function is simply printing all the results that the query would have returned, you will probably want to format this using some html. $data is just an array which you can print a single element from like this:
echo $data['email'];
I hope this helps!
<?php
echo " alert('Record Inserted ');"
OR
echo " document.getElementByID('tagname').innerHtml=$result;"
?>
OR
include 'Your Html file name'