I would like for a user to be able to input data in a text field on my website. WITH this data I would like for them to be able manipulate it.
For example:
Let's say someone needs all the letters in a paragraph capitalized and on my website I have a PHP script that does just that. How do I create a means for them to use my script?
Like so:
paste paragraph into left text field
press 'action button' or in this example 'capitalize letters' button
text in left text field gets ran through the script and becomes all capitalized
text now appears in right text field
A better way to ask this I guess is how do I connect the users input with the script and display the output once it's been ran?
You have to put your fields in a form in the HTML file, for example like this:
<form method="post" action="script_that_does_the_action.php">
Left paragraph: <input type="text" id="leftP" name="leftP"><br>
Right paragraph: <input type="text" id="rightP" name="rightP">
</form>
and then in your script that does all of the action, you can fetch the user input like this:
$userInput = $_POST['leftP'];
//do the capitalization now here
//store the result somehow. Maybe using sessions like this: $_SESSION["result"];
//then you have to redirect the page back to where the text fields are for example using header("location: ");
and now that you are back in the index page (if I may call it like that), paste the resulting value to the right field:
<input type="text" id="rightP" name="rightP" value="<?php echo $_SESSION["result"]; ?>">
Be sure that both of your files (the action script and index file) are in .php format, and that you start the session with session_start();
That's only one example...the most basic one. If you want to make it in the proper way, I'd also suggest using javascript :)
Related
I am making a form for the admin area of WordPress. Here is the code so far;
<form method="post" action=options.php">
<?php update_option('gpspl_options', $gpspl_options);?>
<input type="text" name="gpspl_options" value="$gpspl_options"/>
<input type="submit" value="Save Changes"/>
</form>
On the page in the admin area the text box is auto filled with "$gpspl_options". However when I add the text and hit submit it does not update the wp_options table in the database.
What am I missing?
You always call update_option() on whatever is stored in $gpspl_options. You never do anything with the posted value ($_POST['gpspl_options']). So the posted value never gets saved. If you want to save the posted value, you need something like this:
if (isset($_POST['gpspl_options'])) {
update_option('gpspl_options', $_POST['gpspl_options']);
}
As for the text field, you always initialize the text field to the literal string "$gpspl_options" (not the value of the variable $gpspl_options). To use the value, you need something like this:
<?php
$gpspl_options = isset($_POST['gpspl_options']) ? $_POST['gpspl_options'] : '';
?>
<input type="text" name="gpspl_options" value="<?php echo $gpspl_options; ?>"/>
You might want to read an introductory PHP tutorial covering variables, variable names, output, and so on.
That said, all this mixing of logic and output is not good practice. It's what Wordpress does and therefore kind of encourages, but that doesn't mean you should do it, too.
I am having trouble extracting data from a form. I would like to save email address to a text file to the local server. The user will go firstly to this site (picture),
website
They will type in their email address and a wireless key (controlled by Unifi) and then hit submit. At that point, I would like the email they entered to be saved to a text file. The connect button also authenticates the user with Unifi and they are able to connect to the guest wireless.
The code I have so far is:
<!-- Email Input -->
<fieldset class="large-text">
<p class="form-element">
<label for="email" class="fieldname">Email</label>
<input id="emailcontainer" method="post" name="emailtxtbox" class="textInput" value="" autocomplete="on" input type="email" required placeholder="Enter your email"/>
</p>
</fieldset>
<!-- End of Email Input !-->
This creates the email textbox entry that you see above in the picture.
The below code is the php execute, that will (hopefully) grab the email, and then save it too the text file:
$getemail = $_GET["emailtxtbox"];
$emailtxtbox = $_POST["emailtxtbox"];
if($getemail = "connect") {
$targetFolder = "/emailcollate";
file_put_contents($targetFolder."mytext.txt", $emailtxtbox);
}
As you might of guessed, I have no skill in php or html, so please keep it simple. I have played around the above codes a lot, so some of it may have different names etc. I would appreciate any help!
Does anyone think it would be better to use the file_put_contents() function to do the above?
ok, it has been a long time since i have worked with php, but i will do my best.
the way I would troubleshoot this is make sure the php page actually ran, easily done with:
var_dump("this page ran");
after the page has run, dump the two variables out to the page to confirm that the page has received them and has accurately change them to a variable.
next, in your if statement, dump some random text to make sure the if statement has run.
otherwise, make sure you know where the php file is trying to save to, may have to use this instead:
file_put_contents("/emailcollate/mytext.txt", $emailtxtbox);
the file_put_contents() function should work fine
In PHP $GET is used for picking up parameters in your site's URL, and $POST is used for gathering form values that are submitted to your page.
In your case you're getting the value for $getemail from the URL of your page, so for $getemail to have a value the URL for your site should resemble
www.yoursite.com/?emailtxtbox=connect
Additionally you need to add an extra equals sign in your if statement, like so:
if($getemail == "connect") {
The double equals is a comparator used for comparing two values against each other, rather than a single equals which is used for assigning a value to a variable.
Your file_put_contents looks ok, but like user6063785 has suggested try running that block of code outside of the if statement to see if the file is being written to. You may need to ensure that PHP has permissions to write to the file.
I have written a small php (test.php) that reads and displays the data passed to it.
<?php $name = $_GET['name'];
echo $name;?>
When I execute the following URL
http://localhost/test.php?name=Géo
The text that gets displayed is: G�o
However, when I submit "Géo" from another webpage:
<form method="get" action="test.php">
<input type="test" name="name">
<input type="Submit">
</form>
the URL changes to http://localhost/test.php?name=G%C3%A9o
and now the correct text is displayed:Géo
In the first scenario, only when I add utf8_encode( $_GET['name']), the correct text (Géo) gets displayed.
My question:
Is it possible to know how the data was passed, submitting via a form or directly from the URL. If not then what changes should I make to the webpage so that both the scenarios are handled correctly i.e. display Géo in both the cases.
Thanks for your help.
You can add a reg. expression that will check if the parameter contain %. Because that the encoded parameter probably contains a %.
But, if you'll execute url that for some reason contains a %, the problem will comeback...
After alot of digging around some very informative posts and info to try and find out how to solve this issue I thought I would ask around to see if anyone has any pointers.
I have an html form with various inputs (checkboxes, text boxes etc...). Each input section has its own submit or 'Upload' button. On Upload a php script is called and various bits of processing is done before data is sent over a pipe to a Python script for further stuff.
I am currently echoing back input variables to the form on submission so that the html page does not refresh (or should I say the inputted data is not lost to the users view) on an Upload event, however, I now have to do the same for a bunch of checkboxes and text boxes the values of which are stored in an array. The code I have written so far is as follows (I am new to both php and html so please excuse the inefficiency that I'm sure is obvious)
html/php
<margin>CH1</margin><input type="checkbox"name="ANout[]"value="AN1_OUT"
<?php if(in_array('AN1_OUT',$_POST['ANout']))echo'checked';?>>
Voltage<input type="text"size="5"name="ANout[]"
value="<?php $ANout[$i]=$_POST['ANout'];
if(!empty($ANout[$i]))echo"$ANout[$i]";?>">
<br>
The code above works fine for the checkboxes which happily remain after an Upload button is pressed but not for the array. When the Upload event occurs I simply get 'Array' written in the text box. I have tried existing code I have written to echo back other text input in the form (see below) and which works but these are for sole entries, not arrays. I have tried various configurations of syntax but I always seem to get the same result.
Working Code:
<margin>Duty Cyle</margin><input type="text"name="PWM1DC"size="3"
value="<?php $PWM1DC = $_POST['PWM1DC'];
if(!empty($PWM1DC))echo "PWM1DC";?>">
<br>
I'm sure it is something straightforward but I have been fiddling and staring at it for ages and can't seem to find the problem.
You are getting "Array", because you are trying to print out variable of type Array.
You probably want to give your fields separate names or indexes and do something like this:
<form method="post">
<input type="checkbox" name="ANout[1]" value="AN1_OUT"
<?php if(isset($_POST['ANout']) && in_array('AN1_OUT',$_POST['ANout']))echo'checked';?>>
Voltage<input type="text"size="5"name="ANout[2]"
value="<?php if(isset($_POST['ANout']) && !empty($_POST['ANout'][2])) echo $_POST['ANout'][2]; ?>">
<input type="submit" value="ok">
</form>
(Just added form tags, submit button and isset checks to show working example.)
I have a search form on my site,
and I want to pass the text in the form to the URL,
like: mysite.com/search.php?q=apples (if search word was apples).
I figure that way people can bookmark their searches.
One solution I thought would be to catch the searchword in search.php and then reload into a new made URL. But it's not very elegant to reload like that. So how can I do it - I mean, how is it normally done? Do I need to use jQuery?
Clarification: I know how to get the vars from the URL in php. What I need is to control the URL that will be opened when the user presses SUBMIT, and the URL needs to contain the user's search word! Just like Google or DuckDuckGo, I put "apples" and the URL becomes ...?q=apples. But - how?! (Then I'll pick that up in the search.php, of course, but I know how to do that.) This is what I have now:
<div id="topnav">
<form action="search.php" method="post">
<input name="searchword" type="text">
<input type="submit">
</form>
Thank you so much.
Upon reading the clarification. What you need is a search form that submits to your search.php for example:
<form action="search.php" method="get">
<input type="text" value="search word" name="q" />
<input type="submit" value="submit" />
</form>
This will pass whatever value entered in the input named q to the search.php script.
If you post a HTML form which includes a text field with name 'q' and value 'apples' then the URL you want is automatically created by the browser. You definitely don't need JQuery for that.
how about using the POST-Redirect-GET pattern? [http://en.wikipedia.org/wiki/Post/Redirect/Get] also http://blog.andreloker.de/post/2008/06/Post-Redirect-Get.aspx
This would allow you to keep the url in the browser:
yoursite.com/search.php?q=apples
Alternatively, you can use javascript to set the location.hash of the url in the browser w/ the query information after the postback; I suspect this is actually what Google does.
eg,
yoursite.com/search.php#apples
So the form action would be search.php, the field would be called q and the method would be Get?
You should be able to handle all this from the html form if I'm understanding what it is you're trying to achieve.
if you have a form then must have declared form methoed POST/GET
in you search.php you can simply do this $_POST['name of the input field'] to get the word string,
and if you want to pass variable in url then you need to make a link through Link