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;
Related
Good morning, afternoon or night :D
I want to pass my $_GET['area'] variable to a method, but after submitting the form, it only let me pass such variable to the header() method:
Note: This is the variable from the URL I want to pass: /index.php?area=work
I have a index.view.php with a button:
<a href="crearArt.php?area=<?php echo $_GET['area'] ?>" >Add new article</a>
From the URL, the button receives the $_GET['area'] and when I click the button it takes me to the area where I can create new articles, here is the code of crearArt.view.php file:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
//... Bunch of inputs
<button type="submit" name="crearArticle">Create New Content</button>
</form>
When I click on "Create New Content" (submitting the form), the crearArt.php file should get the $_GET['area'] variable and pass it to the $addDataToDB() method but that doesn't happens, if I put a print_r() inside the if condition to check if the variable it doesn't show the variable.
Furthermore, if I pass the variable to the header() method it does work, meaning that (I think) the variable still there.
Here the crearArt.php file
<?php
$inputNames = [];
$inputValues = [];
if(isset($_POST['crearArticle'])){
// Get all inputs from the form
foreach($_POST as $key => $value){
if($key !== 'crearArticle'){
// This will NOT add the button' name property when the form gets submitted
array_push($inputNames, $key);
array_push($inputValues, $value);
};
};
// Add values to WorkDB || CodeDB
$addDataToDB($inputNames, $inputValues, $_GET['area']);
// Return to index.php?area=??
header('Location: index.php?area='.$_GET['area']);
} // END MAIN IF
?>
Sumerizing, when I press on "Create New Content" it should pass the variable to $addDataToDB() but it doesn't do it, even though the URL has the $_GET['area'] var with it: /crearArt.php?area=work
Question:
Is there a way to get this variable without creating hidden inputs in the crearArt.view.php?
Thanks in advance
PS: I'm new into PHP.
In addition
I tried creating a var outside the if conditional and then passing it to the method but that doesn't work either.
By using that variable I'm gonna let the function "know" from which area I come from so the function "know" to which database it has to refer to.
If I use variable as a string ('work'), it does work.
Each HTTP request you make has its own URL.
The request you make to crearArt.php?area=<?php echo $_GET['area'] ?> and the request you make to <?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?> are different requests with different query strings and will have different values in $_GET.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>"
According to the manual, PHP_SELF is:
The filename of the currently executing script, relative to the document root.
You haven't put a query string in your URL.
When you submit the form $_GET['area'] won't exist.
I have the following pages (code fragments only)
Form.html
<form method="post" action="post.php">
<input type="text" name="text" placeholder="enter your custom text />
<input type="submit">
</form
post.php
....
some code here
....
header('Location: process.php');
process.php
on this page, the "text" input from form.html is needed.
My problem is now, how do i pass the input-post from the first page through process.php without loosing it?
i dont want to use a process.php?var=text_variable because my input can be a large html text, formated by the CKeditor plugin (a word-like text editor) and would result in something like this process.php?var=<html><table><td>customtext</td>......
How can i get this problem solved?
I would like to have a pure php solution and avoid js,jquery if that is possible.
If you don't want to use $_SESSION you can also make a form in the page and then send the data to the next page
<form method="POST" id="toprocess" action="process.php">
<input type="hidden" name="text" value="<?php echo $_POST["text"]; ?>" />
</form>
<script>
document.getElementById("toprocess").submit();
</script>
or you can the submit the form part to whatever results in moving to another page.
Having said that using the $_SESSION is the easiest way to do this.
Either use $_SESSION or include process.php with a predefined var calling the post.
$var = $_POST['postvar'];
include process.php;
Process.php has echo $var; or you can write a function into process.php to which you can pass var.
maybe the doc can help: http://php.net/manual/fr/httprequest.send.php
especially example #2:
$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;
}
But I wouldn't use this heavy way where sessions are possible and much easier, see previous answer/comments.
This is ok for instance if you want to call a pre-existing script awaiting post-data, if you can't (or don't want to) modify the called script. Or if there's no possible session (cross-domain call for instance).
How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value
This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.
If this is for validation checks no need for sessions as suggested.
User fills in the form and submits back to self
Sever side validation fails
$_GET
<input type="hidden" name="first"
value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />
validation message, end.
alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....
$_POST
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.
The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.
If $post is a normal variable, then it will never be saved.
If you need to save something, you need to use cookies. $_SESSION is an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.
Reference: http://php.net/manual/en/reserved.variables.session.php
The $_SESSION variable is just an associative array, so to use it, simply do something like:
$_SESSION['foo'] = $bar
You could save your $_POST values inside of $_SESSION's
Save your all $_POST's like this:
<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>
Actually in html forms it keeps post data.
this is valuble when you need to keep inserted data in the textboxes.
<form>
<input type="text" name="student_name" value="<?php echo
isset($_POST['student_name']) ? $_POST['student_name']:'';
?>">
</form>
put post values to session
session_start();
$_SESSION["POST_VARS"]=$_POST;
and you can fetch this value in another page like
session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];
You can use the same value that you got in the POST inside the form, this way, when you submit it - it'll stay there.
An little example:
<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>
You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily
if($_POST['name'])
{
$file = fopen('poststored.txt','wb');
fwrite($file,''.$_POST['value'].'');
fclose($file);
}
if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}
so your post value stored in $value.
I have a PHP file in which I have this code:
$descText = $_POST["fname"];
$attachement = array('access_token'=>$fbme['access_token'], 'message' => $descText, 'source' => '#'.realpath($tempFile) );
//$attachement = array('access_token'=>$fbme['access_token'], 'source' => '#'.realpath($tempFile) );
$fb_photo = $facebook->api('me/photos','POST',$attachement);
$FQLQuery = 'SELECT object_id, pid, src_big, link FROM photo WHERE object_id = '.$fb_photo['id'];
$FQLResult = $facebook->api(array( 'method' => 'fql.query', 'query' => $FQLQuery, 'access_token'=>$fbme['access_token'] ));
$targetPhoto = $FQLResult[0];
echo '<center><h1>Image created.</h1><br/><img src="'.$targetPhoto['src_big'].'"/></center><br/>';
How can I run this only when a user clicks on a button or link?
You can't attach PHP code to a HTML input button, only JavaScript.
If you need to have the code written in PHP then you will either have to link directly to the script, which will display a page with your photo on it (Assuming the code is valid), or more likely what you want is to USE AJAX to include it dynamically in an existing page.
Basically something like this would work in JQuery:
<input type="button" onclick="$.load('/link/to/script.php', $('#where_to_put_the_image'));" />
You could use a form HTML tag. This is just an example:
<form method="post" action="yourphpfile.php">
<input type="text" name="fname" value="your first name?" />
<input type="submit" value="run" />
</form>
Put it in a file and then link to that file. Simple.
Or do you want to run it asynchronously on the page? In which case, you'll need to do it using Ajax.
As with triggering any PHP script or function, send a request to the page that contains it, either by a link, a form post, or an Ajax request.
I've got the following php code printing out the contents of a SQL table.
$query="select * from TABLE";
$rt=mysql_query($query);
echo mysql_error();
mysql_close();
?>
<i>Name, Message, Type, Lat, Lng, File </i><br/><br/>
<?php
while($nt=mysql_fetch_array($rt)){
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file]";
}
}
?>
How would I implement a button so for each "row" if the button is clicked on that row it'll submit the information of that row to another php file?
I want it looking something like...
details details2 details3 BUTTON
details4 details5 details6 BUTTON
details7 details8 details9 BUTTON
details10 details11 details12 BUTTON
Where if BUTTON was hit on row 1 details1,2,3 would be sent to a php file, on row 2 detals 4,5,6 would be sent etc. How would I do this?
Thanks.
it's going to be something like that, depending on the data you need to send:
while($nt = mysql_fetch_array($rt)) {
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file] ".'send request<br/>';
}
}
You can either use GET method and send a query string to the second php page and receive the variables there, like
next.php?variable1=value1&variable2=value2&...
or use POST method by making a hidden form for each row and assign a hidden field for each variable you want to send.
<form method="post" action"next.php">
<input type="hidden" name="variable1" value="value1" />
<input type="hidden" name="variable2" value="value2" />
.
.
.
</form>
or instead of sending all the values, just send the row ID (if any) using any of these two methods and run another query in next.php to get the information you need from database.
Instead of submitting the entire data, just send the ID and fetch the results from the database in the other script. If you want to have an input button, you can do
<form action="/other-script.php" method="GET">
<?php printf('<input type="submit" name="id" value="%s" />', $nt["id"]); ?>
</form>
but you could also just add a link, e.g.
printf('Submit ID', $nt["id"]);
If you really want to send the entire row values over again, you have to make them into form inputs. In that case, I'd send them via POST though.