I had my site working as follows:
Input url in Wordpress options, save and it saves.
Now I want it so set a default value in the input (url of the server) and if I change and save it, on page re-load the new value should appear, not the default value.
Before
<label for="site">
<strong>Site:</strong>
<input type="text" name="url" value="<?php echo $this->getOption($options, 'url') ; ?>">
</label><em>
My attempt
I have now set a default value of the input to be that of the server url but obviously on page re-fresh it will display this value, just unsure how to fix this so if I update the input and press save, on page re-fresh it will retain my last input rather than updating it with the default value.
<label for="site">
<strong>Site:</strong>
<input type="text" name="url" value="http://<?php echo str_replace('www.','', $_SERVER['SERVER_NAME']); ?>/"/>
</label><em>
My suggestion is to set a variable to the original $_SERVER value.
If a value is posted from the form, set the variable to that value instead.
Then, use that variable to populate the input value, rather than the original $_SERVER value.
Something like this:
<?php
// this uses a ternary operator. if ? then : else.
// if value is posted set to that value, otherwise set to $_SERVER value
$url= !empty($_POST['url'])
? $_POST['url']
: 'http://'.str_replace('www.','', $_SERVER['SERVER_NAME']);
// show a message if data is posted (for debugging purposes)
if (!empty($_POST)) {echo "<p>Data was posted.</p>";}
?>
<form action="" method="post">
<label>
<strong>Site:</strong>
<input type="text" name="url" value="<?php echo $url ?>" />
</label>
</form>
Related
I have the following code snippet of my fields I have in my form:
<input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>
This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?
In your HTML, add the input field like this:
<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
Basically, the value attribute of the text field needs to be set to:
<?php echo $_GET['username']; ?>
The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:
$_GET['variable_name'];
Then you can assign it to variables or pass it as a function parameter.
**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **
First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.
You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^
<form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<p>Input</p>
<div>
<input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
</div>
</fieldset>
<div>
<button type="submit" value="Submit" name="submit">
Submit
</button>
</div>
</form>
The <input> tag and other fields of form must be in a <form>tag.
<form action = "https://mysite" method = "get">
<input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
</form>
In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.
Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.
All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.
e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.
I have a set of input boxes and you can add more and more sets of these forms if you click the add more button. In my form I can submit data and I have got it to show up when you reload the page, when the page shows it it also adds a value into a hidden form in case the user updates this information.
However, how can I see all the sets of data which do not have a hidden form value? And all the sets with do have a hidden value so I can do different things to them.
Here is my code:
HTML:
<form>
<div class = "fieldset-1">
<input type="text" id="Name1" name="name[]">
<input type="hidden" id="id1" name="id[]">
</div>
<div class = "fieldset-2">
<input type="text" id="Name2" name="name[]">
<input type="hidden" id="id2" name="id[]">
</div>
</form>
PHP:
$data = $_POST;
extract($data, EXTR_PREFIX_SAME,"br");
//Prints The Variables To Make Sure They Are Correct
print_r($id);
$name = preg_replace("/[^a-zA-Z- ]/", "", $name);
print_r($name);
You have all the post data in a $_POST. It doesn't depend on field's type. The only thing matters — field's name.
The reason why you can't see it with your code is that you do
$name = preg_replace("/[^a-zA-Z- ]/", "", $name);
For what, btw? preg_replace is for string, $name here is an array (cause your form field has a name name[]), so function fails, and you lost your data.
And don't ever use extract, it's considered harmful.
I have a normal form in a PHP page, I send data to the php page from another for using POST. The PHP page runs some scripts to update data to SQL but on that page I have a second form that needs to be completed with data from the initial form prior to updating the SQL.
$recipient_nr = $_REQUEST['recipient_nr'];
Here I draw the info from the first form
Now I want to use this in a new form on the current PHP page how do I state this in the new form
<input type="text" name="recipient_nr" id="recipient_nr" value=".$recipient_nr.">
This is what I am attempting but it is not working I know I have too many "'" xxx"'" in the lines but not sure how to remidy this
Do you generate the new form in PHP? If so, where is the code where you do that?
If this is some kind of ...?> <input type="..."...> <?php ... page generation then you'll need to echo that $recipient_nr into the PHP-generated response:
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?php echo $recipient_nr; ?>">
<?php
...
Or, if you have short echos turned on,
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?= $recipient_nr ?>">
<?php
...
use this:
<input type="text" name="recipient_nr" id="recipient_nr" value="<?php echo $recipient_nr; ?>">
Something like this:
$recipient_nr = array_key_exists('recipient_nr', $_REQUEST)
? (string) $_REQUEST['recipient_nr']
: 'default value or empty string';
And output it:
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?=$recipient_nr?>">
But if you want store this data for/between other pages you can use $_SESSION global array for save it.
I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';
I have several landing pages and the only difference in them is a hidden input's (referred_by_text) value. I would like to just make one form and include it in all the landing pages....how would I go about setting the value for the input on each landing page for that value?
In the landing page this:
<?php include("includes/lp_form.php");?>
In the included form:
<input type="hidden" name="referred_by_text" value="" />
I would like to set the value for "referred_by_text" in each landing page...not sure where to start, any help is much appreciated. Would it be best to do it as an variable or possibly in JS?
Alter "includes/lp_form.php" to expect a variable for the "referred_by_text" value:
<input type="hidden" name="referred_by_text" value="<?php echo $referred_by_text; ?>" />
Then set the value in your page-specific script before including "includes/lp_form.php"
$referred_by_text = 't2w'; // Or other dynamic value, as needed
include "includes/lp_form.php";
You can use PHP to echo out the value you want. You could try something like:
<input type="hidden" name="referred_by_text" value="<?php echo $_SERVER['HTTP_REFERER'] ?>" />
That will appear blank if there was no referer.
In the include that contains the form with the hidden text field, you could set the value based on the page that is visited. For example, in the include file you can create an array of your landing pages and then
$landingPages = array('landingpage1' => 'referred_by_text_value', 'landingpage2' => 'referred_by_text_value', 'landingpage3' => 'referred_by_text_value');
$page = $_SERVER['PHP_SELF'];
if(array_key_exists($page, $landingPages)) {
echo '<input type="hidden" name="referred_by_text" value="'.$landingPages[$page].'" />';
}
This will put the value you want based on the array key into the hidden field.