Allow user to paste in "Tab-delimited" data into a textarea - php

My question is how do I allow a user to Paste in tab delimited data into a textarea on a website and then store and parse that data to run some action against it.
For example at http://www.batchgeo.com you can paste in your address location and from that a map is generated via the google maps api. I'm not looking for a step by step answer just what is happening on the server side of things to store and parse the data.

To receive data into a PHP script, just set up a form that POSTs to it:
<form action="script.php" method="post">
<textarea name="data" wrap="virtual"></textarea>
<input type="submit" value="Go!" />
</form>
Next, create the PHP script to receive the data:
<?php
$data = $_POST['data'];
# do something with $data...
?>
What comes next depends on what you want to do with that data.

Related

How to get page body after POST request

I'm trying to make a PHP page that lets you upload a document to telegram servers and than retrives a file_id which is stored in the json of the redirect page. After some tutorials on YT this is the code:
<?php
$botToken = "1099xxxxxx:AxxxE-g9qDI2Uxxxxxxxxxxxxxxxxxxxxxxxx";
$website = "https://api.telegram.org/bot".$botToken;
?>
<form action="<?php echo $website.'/sendPhoto' ?>" method="post" enctype="multipart/form-data">
<input type="text" name="chat_id" value="mychatid">
<input type="file" name="photo">
<input type="submit" value="send">
</form>
Redirect after submit
The goal is to retrive the file_id of the file and save it in a variable, possibly without opening another tab.
Thanks!
You've making the request to the Telegram API from the browser by submitting a form directly to it.
There is no way your PHP can get any information from that API that way.
As a side effect, you are giving your token (which should be kept secret) to every visitor who uses the form.
You need the browser to submit the form to a PHP program you control and then make the request to the Telegram API from your PHP and not from the browser.
The usual way to do this is with the cURL library.

Cab PHP create an Input field?

I'm trying to create a cookie for a web page. The cookie value will vary based on the users name. Does PHP have an input type function? I just want to add an input field to the page an then the PHP will use that to define the users name for the page. I have the create cookie code, just can't figure out how to get the name from the screen and insert it to the cookie code. Appreciate any suggestions. This is on a WP website.
Not natively because php does not execute in browser, it executes on your server, but it can be used to write an HTML input.
The syntax would look something like this:
echo '<input type="text" name="myinput">';
or
?>
<input type="text" name="myinput">
<?php
You would then use a form post, CURL, or AJAX function to send the data back to the server where a second PHP script would process the input.
That said, it would help to post your create cookie code, since you may not even need to send it back to the server, but just handle it all in the browser using Javascript in which case your submit button only needs to pass the input to a Javascript function instead of posting it.
Is this something you are looking for?
Here it just takes the value user input from the browser and set it as a cookie
<?php
if(isset($_POST['name']) && !empty($_POST['name'])){
setcookie('setcookie_name',$_POST['name']); // setting cookie
}
?>
<form action="" method="post">
<input name="name" value="" placeholder="Enter your name" />
<input name="submit" type="submit" value="Submit"/>
</form>

How do I get the input of a text box and write it to a .txt file with PHP?

Original:
I am trying to make the PHP get the input that was put into the text box and write it to name.txt. I am also getting an error message that says "Expected tag name. Got '?' instead." on line 6.
<html>
<body>
<p>Enter name here:</p>
<input type="text" id="name"/>
<button onclick="[activate PHP]">Enter</button>
<?php
$fp = fopen('name.txt', 'w');
fwrite($fp, '[name entered]');
fclose($fp);
?>
</body>
</html>
I am not familiar with PHP, so please explain what your fixed code does when you answer.
New:
This was made by #Vlad Gincher. The problem is, the code is not creating a .txt file, which leads me to believe that the PHP is executing as soon as the page loads. Is there a way to activate the PHP when the form is submitted?
<?php
if(isset($_POST["textareaValue"])) {
$fp = fopen('name.txt', 'w');
fwrite($fp, $_POST["textareaValue"]);
fclose($fp);
}
?>
<html>
<body>
<p>Enter name here:</p>
<form method="post">
<input type="text" id="name" name="textareaValue" />
<input type="submit" value="Enter" />
</form>
</body>
</html>
Again, I am inexperienced with PHP, it could be an error of mine.
PHP runs on the server side. After the server finishes, it returns the output to the client. In that point, you can't use PHP, but you can force the client to send another HTTP request so the PHP would be activated.
Here is a code where it runs, checks if the client sent information to the server, and if so, add the information the the file. If not, it does nothing.
I'm using HTML's form element to tell the browser to send the information back to the server using post. Then, I can get the value using PHP's $_POST, and with the name of the input that I want to get the data from (textareaValue)
<?php
if(isset($_POST["textareaValue"])) {
$fp = fopen('name.txt', 'w');
fwrite($fp, $_POST["textareaValue"]);
fclose($fp);
}
?>
<html>
<body>
<p>Enter name here:</p>
<form method="post">
<input type="text" id="name" name="textareaValue" />
<input type="submit" value="Enter" />
</form>
</body>
</html>
When the web page has been rendered (displayed to user), the PHP is finished. PHP cannot interact with a user - for that you need either (a) an HTML form that is submitted to a .php file (even the same one that contains the form), or -- and this is far more popular these days -- javascript with AJAX.
Here is another answer that discusses both:
How can I make, when clicking a button, send an email with the data included from the form?
HTML forms get "submitted" to a back-end PHP file that receives the data from the form fields. Each form field has a name= attribute on the HTML tag -- that becomes the variable name, and the contents of the HTML element becomes the variable's data.
A very similar thing happens with ajax, except that it is more flexible (the HTML form system is very rigid/static), and (most importantly) the web page need not refresh.

Feeding a list of URLs from an HTML form to PHP array

I have a .php file which I will be using to submit requests to a certain API. This API will return information regarding certain domain URLs, such as the domains age, PageRank, etc.
The part of the PHP file which is responsible for feeding the API call URL with the domain names I'm interested in looks as follows:
$batchedDomains = array('www.example.com', 'www.cnn.com', 'www.apple.com');
What I would like to do is feed this array information through a very simple HTML form. My current HTML for the form looks as follows:
<form name="myform" action="apitest.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
URL List:<br />
<textarea name="urls" rows="20" cols="60">Enter URLs</textarea><br />
<input type="submit" />
</form>
Here is what I would like to see happen: whenever I enter a list of domain URLs into the HTML form (one domain per line), I would like the $batchedDomains array to be populated with those values.
Can anyone help me out with this? Or if you have a suggestion for a different solution I'm of course willing to hear it out.
I do not want this information printed anywhere, as it will simply be used by the php script to call the API and display the results.
Thank you.
$urls = array_filter(explode(PHP_EOL, $_POST['urls']), 'parse_url');
Or pass a custom callback with filter_var() + FILTER_VALIDATE_URL for stricter checks
If you're just entering one per line, you can split the textarea string by newlines, see https://stackoverflow.com/a/1483501/2213444.
<?php
// has no error checking, you'll want to check that $_POST['urls'] exists
$textarea = explode("\r\n", $_POST['urls']);
print_r($textarea);
?>
<form action="" method="post">
<textarea name='urls' rows='20' cols='60'>
</textarea>
<input type="submit">
</form>
Working Example

how to Pass Parameters to php page without having to load it

how can i pass parameters from an html page(map.html) to a php(createxml.php) without having to open the php page? Im incorporating googlemaps in html page(map.html) so i want the users to enter data on a form on the html page which will be sent to php(createxml.php) which in turn will connect to mySQL DB and create an xml format of the response the html page uses this xml output to create positions on the map since it contains longitude and latitude.
I have used the following code in the heading of the php page(createxml), but it shows the contents of php file for a brief moment redirecting me to map.html
Thanks for your time, i can post all the code if needed.
<meta http-equiv="refresh" content="0;url=http://localhost/map.html/">
It's quite simple with AJAX, using jQuery you don't have to know much about it :)
So simply import the latest jQuery Library.
Then you have your form:
<form id="my_form">
<input type="text" name="param1" />
<input type="text" name="param2" />
<input type="hidden" name="action" value="do_stuff" />
<input type="submit" value="Submit" />
</form>
and somewhere beneath that, you just paste this tiny javascript-function, which handles the submit of the form:
<script>
$('#my_form').submit(function(){
var post_params = $('#my_form').serialize();
$('#waiting').show();
$.post('the_page_you_are_on.php', post_params, function(data) {
$('#waiting').hide();
return false;
})
});
</script>
(The element (div, p...) with the id "waiting" could e.g. contain one of those fancy ajax loading images, but is not neccessary! :) If you want one to be shown, find one via google, set it as the background image of the #waiting-element and set its display to none (CSS)).
The function itself just calls the page you're on and then you've got the form variables in your post-array, so the top of your page could look something like this:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'do_stuff' :
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//do some DB-stuff etc.
break;
}
}
?>
I hope that helps!
It's a terrible idea, but because you don't want to use AJAX you could put the PHP in a frame and reload just that portion. Again, awful idea, but the closest you're going to get without using AJAX.
On a useful note though, AJAX is literally just one function in javascript. It's not hard at all to learn.
If you are just trying to pass parameters to a PHP page from the web browser, there are other ways to do it beyond 'Ajax'. Take a look at this page and view the source code (be sure to view the source of the included javascript file: http://hazlo.co/showlist.php?s=chrome&i=4e289d078b0f76b750000627&n=TODO
It uses an extremely basic method of changing the src of an image element, but passes information to the web server (PHP page) in the querystring of the image request. In this example I actually care about the results, which are represented as an image, but it sounds like you are just trying to pass data to the server, so you can return a 1 pixel image if you like. BTW, don't be fooled by the URL that is being used, a server rule is telling apache to process a specific PHP file when check it,GIF is requested.
You should play with it and use firebug or chrome's built in debugger to watch the requests that are being sent to the server.
You can't get any results from a PHP-script if you don't request it and process the output. If you dont't want to leave the current page, you have to use AJAX!
"but it shows the contents of php file for a brief moment" The reason is, that your browser first needs to load the entire page, then start the META-redirect. You don't need a redirect to load data from the server, but if you really want to, you should HTTP-headers for redirect.
Ok guys after hours of headache i finally found the solution! Basically i called my xmlproduce.php from inside my map.html, lemme explain so maybe will help others:
maps.html contained a googlmap API Javascript function which called my createxml.php called second.php
GDownloadUrl("second.php", function(data) )
what i did was i tweaked this call to second.php by adding some parameters in the URL like:
GDownloadUrl("second.php?strt="+ysdate+"/"+msdate+"/"+dsdate+"&end="+yedate+" /"+medate+"/"+dedate+"&id="+ide, function(data)
which is sending the parameters needed by second.php, so after that i made a small html form which called the script of googlemap api on the same file(map.html) to pass the parameters from the form to the GDownloadUrl i mentioned above, the code of the form is :
form method="get" action="">
IMEI: <input type="text" id="id" name="id" size="25" /> <br />
Start Date: <input type="text" id="ysdate" name="ysdate" size="4" value="2000" /> <input type="text" id="msdate" name="ysdate" size="1" /> <input type="text" id="dsdate" name="dsdate" size="1" /> <br/>
End Date: <input type="text" id="yedate" name="yedate" size="4" /> <input type="text" id="medate" name="ysdate" size="1" /> <input type="text" id="dedate" name="dedate" size="1" /> <br/>
<input type="button" value="submit" onClick="load()" />
</form>
afterwards i put extra constraints on the form for the values allowed.
Thanks everybody for the help, and you can always ask if somebody needs some clarification.

Categories