Append an input's value in included form..PHP - php

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.

Related

How do I use get method to input values to form field with PHP

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.

Add data to sql on button click

my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>

PHP default input value won't change on save

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>

How to get url id from another page by form action page

i have a page where i am getting the ques id and insert it in the database for that i am doing
url: */faq/faq_question_sol.php?ques= 62*
this ( $selected_ques= ($_GET['ques']); ) is working properly in the *faq_question_sol.php* but the *answer_submit_process.php* does not recognize it
my form
<form id="post-form" class="post-form" method="POST" action="answer_submit_process.php">
<input id="submit-button" type="submit" tabindex="120" name="submitbutton" value="Post Your Answer" />
</form>
and the *answer_submit_process.php* is
if(isset($_POST['submitbutton'])){
$userid = $_SESSION['userid']; // i have already started the session
$selected_ques= ($_GET['ques']);
$content = $_POST["content"] ;
$query="INSERT INTO `formanswer`( `user_id`,`questionid`,`content` ) VALUES ('{$userid}','{$selected_ques}','{$my_html}' ) ";
$result=mysql_query($query);
}
Quickest solution would be saving the value of $_GET['ques'] on a hidden field of the form and thus make it accessible in answer_submit_process.php.
Something like this:
if (isset($_GET['ques'])){
echo '<input type="hidden" name="ques" value="'.$_GET['ques'].'">';
}
And in answer_submit_process page the value could easily accessed by $_POST['ques']..
If you are sending via a form POST, then the variable from which you can get data is $_POST instead of $_GET.
Anyway, i wasn´t able to find any field relating to the ques variable on your form, where are they?
Add <input type="hidden" name="ques" value="<?php echo $_GET['ques'] ?>"/> to your form to temporarily store the variable, and then use the variable $_POST['ques'] in place of $_GET['ques'] in the processing page.
Alternatively, you could change the form action to answer_submit_process.php?ques=<?php echo $_GET['ques']; ?>.

Basic problem of getting values of my textbox

I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"

Categories