Say we have page 1:
<?php
$text=$_POST['text'];
echo 'You wrote "' . $text . '".';
?>
and page 2, which takes the user input, POSTs it to Page1, and gets the page (You wrote (input).). I'm not really sure about how to do this: googling (or better, stackoverflowing) a while, I found this question, which explains how to POST a variable to another page, but not how to get the code of the page. So, how to do both?
EDIT: An example of the actual situation.
The user inputs the number 245. I get the number, pass it to this external page, get the page, retrieve the result (5*7*7) and show it.
In italics you see the part I need.
I'd suggest using curl to POST the data, and then you'll probably need to parse the returned HTML.
Here's a blog post about how to use cURL to post. I'll leave using google to find an example of parsing HTML with PHP as an exercise for the reader . . .
page 1:
<form method="post" action="page2">
<input name="input" type="text"/>
<input type="submit" value="sendtopage2"/>
</form>
page 2:
<?php
$text=$_POST['input'];
echo 'You wrote "' . $text . '".';
?>
Working example with cURL:
$function="sin(x)";
$var="x";
$url='http://www.numberempire.com/integralcalculator.php';
echo '<base href="' . $url . '">'; // Fixes broken relative links
$Curl_Session = curl_init($url);
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "function=$function&var=$var");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 0);
$content=curl_exec($Curl_Session);
curl_close ($Curl_Session);
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.
Here is my current code:
<?php
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
?>
My PHP code works, but here's the line I'm having issues with:
$urls=array('http://www.site1.com','http://www.site2.com/');
Basically I just want to have a text-area on my website where users can enter a list of URLs (one per line) and then it Posts it using the PHP code above.
My code works when I just have the URLs built into the code like you can see above, but I just can't figure out how to make it work with a text-area... Any help would be appreciated.
You can do it like below (on the same php page):-
<form method = "POST">
<textarea name="urls"></textarea><!-- add a lable that please enter new line separated urls -->
<input type = "submit">
</form>
<?php
if(isset($_POST['urls'])){
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent as new-line separated string and explode it to an array
$urls=explode('\n',$_POST['urls']); //Or $urls=explode('\\n',$_POST['urls']);
// if not worked then
//$urls=explode(PHP_EOL,$_POST['urls']);
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
}
?>
Note:- you can separate the form and logic on two different pages. that's seay i think.
You can use text-area and then you have to then explode by \n or if not work then explode by PHP_EOL to the initial url string and rest the code is same as above
You can pass to the form value as in bellow box is. any tag in HTML form have name attribute, assign name for any textarea, input or select tag.
<form method="post" name="first_form">
<input type="text" name="url" value="" />
<textarea name="note"></textarea>
<input type="submit" value="Post" />
</form>
In PHP you can the pass the value using two methods, $_GET or $_POST.
assign the tag name which you give for the HTML tag ($_POST['note'] or $_POST['url']) and also you can make it like an array $_POST.
<?php
if(isset($_POST)){
// display the posted values from the HTML Form
echo $_POST['note'];
echo $_POST['url'];
}
?>
Your Code:
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
Yesterday I asked a question about how I could go about hijacking my MailChimp contact form to send an additional email (when certain conditions are met).
I wrote changed the action="... from the MailChimp URL to my own process_email.php with code resembling the following:
extract($_POST);
$url = 'http://myMailchimpUser.us2.list-manage.com/subscribe/post';
$fields = array(
'u' => urlencode($u),
'id' => urlencode($id),
'group' => http_build_query ($group),
'MERGE1' => urlencode($MERGE1),
'MERGE2' => urlencode($MERGE2),
'MERGE3' => http_build_query($MERGE3),
'other_more_info_text' => urlencode($other_more_info_text),
'submit' => urlencode($submit)
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
I have yet to add the code which sends the email to myself, but that should be trivial. The problem I'm having with this code is that instead of redirecting me to the MailChimp page, its actually loading within process_email.php (and its loading incorrectly to boot.)
I know I can accomplish what I want to do if I used JavaScript, but it seems to me that isn't the proper way to go about this. Can anyone offer me any help?
If I understand correctly: you want to POST data locally first then let the form POST to Mailchimp. If that is what you are trying to do, then using some JS connected to the form (or a form button) is probably the best way to go. I think it is the proper way to go in your situation.
Something jQuery like below would work to POST the form locally first, and once that request is complete it would submit the form using the given action url (mailchimp).
$(document).ready(function(){
$('#submit-button').click(function(event){
event.preventDefault();
$.post("your_email_parser.php", $('#form-id').serialize(), function(){
$('#form-id').submit();
});
});
});
...
<form id='form-id' action='http://myMailchimpUser.us2.list-manage.com/subscribe/post' method='post'>
...
<input type='submit' id='submit-button' />
</form>
I need help in trying to use curl to post data to a page and retrieve the results after the form has been submitted.
I created a simple form:
<form name="test" method="post" action="form.php">
<input type="text" name="name" size="40" />
<input type="text" name="comment" size="40" />
<input type="submit" value="submit" />
</form>
In addition, I have php code to handle this form in the same page. All it does is echo back the form values.
The curl that I have been using is this:
$h = curl_init();
curl_setopt($h, CURLOPT_URL, "path/to/form.php");
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, array(
'name' => 'yes',
'comment' => 'no'
));
curl_setopt($h, CURLOPT_HEADER, false);
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($h);
echo $result;
When I launch the page with the curl code in it, I get the form.php page contents but it doesn't not show
the variables that PHP should have echo'd when the form is submitted.
would appreciate any help with this.
Thanks.
AHHHH after your comment, the problem is clear.
you need to include one more POST variable in your array.
'submitted' => 'submitted'
the submit button also returns a post value if clicked, which you are checking in your form processing PHP.
if ($_POST['submitted']) {
in your curl code however you have left out the 'submitted' post variable.
if this is what you are looking for, please select the check mark next to this answer. thanks!
From reading the other two answers, and the comment by the OP, I have a couple of ideas.
Specifically, the OP Comment was:
Submitting the form manually does produce the right output. The PHP that handles the form is: if(isset($_POST['submitted'])){echo $_POST[name] ..... etc. and that's it
Under standard conditions, your basic form, as noted in the Original Question, would generate a $_POST array like the following:
array(
'name' => 'The Name as Entered in the Form' ,
'comment' => 'The Comment as Entered in the Form' ,
'submit' => 'submit' # From the "Submit" button
);
Your comment suggests that some aspect of your form handler is looking for a $_POST element called "submitted".
1) The basic form, as set out in the question, will always return FALSE for a check for $_POST['submitted'], and, as such, will trigger the ELSE action (if present) for that conditional.
2) Your CURL Action does not set either 'submit' or 'submitted', and, again, will always return FALSE for the conditional.
So, I would suggest that you:
1) Check your Form Handler, and see what fields are essential, what their names are and what their content should be.
2) Check your Basic Form, and ensure that it has the appropriate fields.
3) Check your CURL Action, and ensure that it details every field which is required. (Easy check would be to insert print_r( $_POST );die(); at the top of your Form Handler and submit the basic form. This will show you exactly what the form is sending, so you can recreate it in CURL.
can anyone tell me how to read up to 3 remote files and compile the results in a query string which would now be sent to a page by the calling script, using headers.
Let me explain:
page1.php
$rs_1 = result from remote page a;
$rs_2 = result from remote page b;
$rs_3 = result from remote page c;
header("Location: page2.php?r1=".$rs_1."&r2=".$rs_2."&r3=".$rs_3)
You may be able to use file_get_contents, then make sure you urlencode the data when you construct the redirection url
$rs_1 =file_get_contents($urlA);
$rs_2 =file_get_contents($urlB);
$rs_3 =file_get_contents($urlC);
header("Location: page2.php?".
"r1=".urlencode($rs_1).
"&r2=".urlencode($rs_2).
"&r3=".urlencode($rs_3));
Also note this URL should be kept under 2000 characters.
Want to send more than 2000 chars?
If you want to utilize more data than 2000 characters would allow, you will need to POST it. One technique here would be to send some HTML back to the client with a form containing your data, and have javascript automatically submit it when the page loads.
The form could have a default button which says "Click here to continue..." which your JS would change to "please wait...". Thus users without javascript would drive it manually.
In other words, something like this:
<html>
<head>
<title>Please wait...</title>
<script>
function sendform()
{
document.getElementById('go').value="Please wait...";
document.getElementById('autoform').submit();
}
</script>
</head>
<body onload="sendform()">
<form id="autoform" method="POST" action="targetscript.php">
<input type="hidden" name="r1" value="htmlencoded r1data here">
<input type="hidden" name="r2" value="htmlencoded r2data here">
<input type="hidden" name="r3" value="htmlencoded r3data here">
<input type="submit" name="go" id="go" value="Click here to continue">
</form>
</body>
</html>
file_get_contents certainly helps, but for remote scripting CURL is better options.
This works well even if allow_url_include = On (in your php.ini)
$target = "Location: page2.php?";
$urls = array(1=>'url-1', 2=>'url-2', 3=>'url-3');
foreach($urls as $key=>$url) {
// get URL contents
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$target .= "&r".$key."=".urlencode($output));
}
header("Location: ".$target);
You can use the file_get_contents function.
API documentation: http://no.php.net/manual/en/function.file-get-contents.php
$file_contents = file_get_contents("http://www.foo.com/bar.txt")
Do note that if the file contain multiple lines or very, very long lines you should contemplate using HTTP post instead of a long URL.