I am trying to create my form to $_GET for bookmarks later down the road. Currently whenever I have tried to $_GET my value, it always sends me to the page I made default on load. One thing I have tried was using a hidden value Using this line of code (Commented it out in my example below of where I tried it):
<input type='hidden' value='<?=$topic?>'/>
I have a web url:
https://mywebpage.com/testphp.php?topic=home.php
The reason for the topic=home.php is that I am going to be switching pages all from the testphp.php page and each will have their own seperate form. If the webpage is loaded with topic=null or topic = "", then it will default to home.php.
When I am creating a HTML Form (Pretty Basic for now) on home.php, omitted everything except the form:
<form id = "test" method = "GET">
<!--<input type='hidden' value='<?=$topic?>'/>-->
<input type="text" name="firstname" Value = '<?=$fname?>' onchange="rememberField(this)">
<input type="Submit" name="Search" >
</form>
Using $_GET for "firstname" on my subpage.php
<?php
include 'index.php';
$fname = "";
$reqmethod = $_SERVER["REQUEST_METHOD"];
if($reqmethod == "GET") {
$fname = $_GET["firstname"];
}
?>
Gathers the "firstname" as it should, and use it to input into the SQL that I create, but the thing it doesn't do is maintain the firstname=ben part.
Instead the new web url looks like this, which will default to home.php:
https://mywebpage.com/testphp.php?firstname=ben
The expected result that I want is:
https://mywebpage.com/testphp.php?topic=home.php&firstname=ben
You missed the name attribute:
<input type='hidden' value='<?=$topic?>' name='topic'/>
P.S. this approach of including server side scripts are venerable to security attacks, so beware!
Just take and example if someone manages to inject this topic=http://hacker.com/erase-all-pages.php
In your hidden field you must set a name for the input
<input type='hidden' value='<?=$topic?>'/> <!--BAD INPUT-->
<input type='hidden' name="topic" value='<?=$topic?>'/> <!-- WITH NAME ATTRIBUTE -->
Related
I am working on a PHP project - I had one form post a date to another form
I made some changes (although none to the input in question)
Now all other inputs are updated with their Posted values, except the date
If I manually set the date in HTML it works:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="2009-01-01"></div>
If I set it to the following, it doesn't:
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo (isset($DateCourse))?$DateCourse:'';?>"></div>
The below:
$DateCourse = ($_POST["DateCourse"]);
var_dump($_POST["DateCourse"]);
var_dump($DateCourse);
Returns:
string(10) "2019-01-05" - means the post value is set
Notice: Undefined variable: DateCourse in /home/bitecons/bts.biteconsulting.co.za/v2/editccr.php on line 119 - how can it be undefined, I just defined it
NULL
What on earth am I doing wrong? Apart from using PHP :P
Flow as requested:
Records.php:
This is the function to prepopulate my posted fields:
function Prefill(x) {
TabletoEdit = x.closest('table').id;
SelectedRow = x.rowIndex;
document.getElementById("EntryEditing").value = x.cells[19].innerHTML;
document.getElementById("DateCourse").value = x.cells[0].innerHTML;
document.forms["records"].submit();
}
Then I also have:
<form action="editrec" method="post" id="records">
<input type='hidden' name='Period' id='Period' />
<input type='hidden' name='Month' id='Month' />
<input type='hidden' name='res' id='res' />
<input type='hidden' name='CustName' id='CustName' />
<input type='hidden' name='DateCourse' id='DateCourse' />
</form>
The Prefill gets called, then submits the form
I have tracked and DateCourse has data, but when getting to the other form, it "disappears":
if(!empty($_POST)) {
$DateCourse = ($_POST["DateCourse"]);
$CustName = ($_POST["CustName"]);
}
For example, CustName is filled in, but not DateCourse?
Side question:
Would this return true if another post var is not set (unrelated to this one):
if(!empty($_POST))
I think You use wrong Code
you Must Submit First Form And Then Use $DateCourse this In another Form in POSTBACK
One of the best way is to define $DateCourse too like
<?php
$DateCourse = "";
if(!empty($_POST["DateCourse"])) {
$DateCourse = ($_POST["DateCourse"]);
}
?>
<div><input type="date" class="form-control" id="DateCourse" name="DateCourse" value="<?php echo $DateCourse;?>"></div>
Okay, apologies folks, but it may help others in the future.
I had a function call to an old function - this failed, causing my variable to never get defined... I knew it was something stupid, but sometimes one needs a sound board...
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 seen many questions related to auto fill, but none of them worked for me. I'm not even sure if a solution to my problem lies in HTML or PHP. I am new in both of them and I'm still not used to them. I'm working on a simple chat app. What I have now is chat window, text field, and nick name field. Both of them pass the values to the text file (which is how I want this to work, no change here). Problem is that both these fields work as a form, and each time I submit, nick name field refreshes. What I would want is auto completing nickname field so it stays the same (it'd be the best if it stayed even after browser refreshes, but it will be okay if it only goes through the form submit)
code if needed:
PHP:
<?php
$action = $_GET["action"];
$myText = $_POST["mytext"];
$nick = $_POST["nick"];
if($action = "save") {
$targetFolder = "/var/www/html/xami/";
file_put_contents($targetFolder."htmlinput.txt", $nick.">".$myText);
}?>
HTML:
<form action="?action=save" name="myform" method="post">
<label for="nick">Nick:</label>
<input type=text id="nick" name="nick" placeholder="Nick" value="Name" required><br>
<input type=text name="mytext" placeholder="Text" required>
<input type="submit" value="Send.">
</form>
I was fooling around with autocomplete but no positive results.
I'm leaving post for tomorrow, I'll reply then.
Assuming your HTML is on a .php page you could have...
if(0<strlen($nick)){
echo "<input type=hidden id='nick' name='nick' value='$nick'>";
}else{
echo "<input type=text id='nick' name='nick' placeholder='Nick' value='Name' required><br>";
}
Or, use AJAX as #Rishi says.
I have a value coming from another form in the same page called $_POST['serial']. And i want to use this value to run a query in another form but after I submit the second form nothing happened and the query not running.
<?php
if (isset($_POST['serial'])) {
$serial = $_POST['serial'];
?>
<form action="" method="post">
<button type="submit" name="submit">Click to use</button>
</form>
<?php
if (isset($_POST['submit'])) {
$query = mysql_query("UPDATE table_name SET status = 'inactive' WHERE serial = '$serial'");
}
}
?>
To pass the variable along you would create a hidden input on your second form to contain the value:
<?php
// check and clean up the passed variable
$serial = isset($_POST['serial']) ? htmlspecialchars($_POST['serial']) : '';
?>
<form action="" method="post">
<input type="hidden" name="serial" value="<?php echo $serial; ?>" />
<button type="submit" name="submit">Click to use</button>
</form>
For Safety's Sake
Your script is at risk for SQL Injection Attacks.
If you can, you should stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really not hard.
Additional Thoughts
If you're planning to do a two-step form you'll likely want to place all of the data processing outside of the form page, in a separate PHP file. With the limited code that you have shown I fear that we will miss something in our answers which will lead you to additional questions because your code still isn't working as you would expect.
A button needs a name and a value to be successful. Your button doesn't have a value so $_POST['submit'] will be undefined.
Add a value attribute to your <button> element.
After you do that, $serial will be undefined because your form doesn't submit that.
You need to include it in your form too:
<input type="hidden" name="serial" value="<?php echo htmlspecialchars($serial); ?>">
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';