PHP Autofill or something? - php

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.

Related

Multiple forms using PHP engine on the same page are submitted all in one go

I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.
I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.
single.php:
echo
'<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;
<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>
emailForwarding.php:
if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
if(isset($_POST['comment'])):
$EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.
The form is submitted the same number of times as the number of the forms on the page.
Inserting include() before tag of single.php disabled duplicate submission.
Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.
Is the echo statement user any loop. Can you try by giving a different name to FORM?
<form method="post" action="" name="form-$i" class="commentForm">

PHP $_GET removing a variable from URL string

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 -->

PHP with input form with text - do a MySQL query when form focus changes

I am looking for some help about how to make input form handling in PHP.
What I need is when a user writes data into a text form (table1), and moves to another text form (like pressing TAB, or selecting with mouse), then it should start and MySQL query to see if such data written at table1 already existing in the matching MySQL table.
My goal is like to do it without pressing submit button. Something like when google checks if an username you want to register already exists.
I am thinking about something like this:
$duplicate_data_test ="";
if (focus has moved to another form field - how to check ?) {
$query = "SELECT table1 FROM testdatabase WHERE table1 = "' . (table1 from the form below - how to get data from the this form field without POST?) .'";
$result = mysqli_query($con,$query);
if(mysqli_num_rows($result)>0) {
$duplicate_data_test = "This data is already found in the database. Choose something else";
}
}
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">';
echo '<input type="text" maxlength="30" name="table1">';
echo '<span class="duplicaterror">'. $duplicate_data_test.' </span>';
echo '<input type="text" maxlength="30" name="table2">';
echo '<input type="submit" value="OK">';
echo '</form>';
Thank you very much for your help!
You cannot do your "interface" check with php.
Your "focus has moved to another form field" has to be done with javascript.
First, Build your form with html like this
<form name="testForm" action="postForm.php" method="POST" id="myForm">
<label>INPUT 1 : </label>
<input type="text" id="in1" value="" name="input1" />
<label>INPUT 2 : </label>
<input type="text" id="in2" value="" name="input2" />
<div style="color:red;font-weight:bold;" id="error"></div>
<button id="submitButton">SUBMIT</button>
</form>
Then make your checks when user clicks on submit button with javascript/jquery & ajax (prevent event form posting) like this :
$(document).on('click','#submitButton',function(event){
event.preventDefault();
if($.trim($('#in1').val()) == ''){
//input 1 is empty
$("#error").html('INPUT ONE IS EMPTY');
}//....continue checks
Finally, if your checks are good, then post your form
$("#myForm").submit();
and if your checks are not good then display user a message!
$("#error").html("MESSAGE!");
I made you a little example on how to do it (it's not the best way to do it but it's just an example) on jsfiddle, check this link : http://jsfiddle.net/9ayo89jt/2/
hope it helps!
checking if something exists will need an AJAX call
put the query that checks the database in a separate php file and call it with AJAX
to submit once all input fields are filled, you will need to use javascript .. check if field 1,2,3,..etc. are not empty .. formName.submit()
this is a bad approach in my opinion

Sowing an updated value in a HTML page

I'm trying to use AJAX to make my website a little slicker than it currently is.
I have a block of code that shows the following
if($guess == 0){
echo "Enter a guess:;
*AJAX form* ---> inserts a MySQL database record with the users guess
<div to show results of script for form> - not necessarily needed
}else{
echo "Update your guess:";
*SAME AJAX form* ---> updates the MySQL database record with the users new guess
<div to show results of script for form> - - not necessarily needed
}
The problem I'm having is that I want part of the webpage to show:
Your latest guess is: £x.xx
However because a user will start off with 0 guesses, and AJAX sends the form "behind" the scenes, I'm struggling in knowing how to show the above line once the user has made a guess.
So also that way when they revisit the page at a later date, it shows the last guess they had.
Those are the only 2 elements of the page I want to be able to refresh, the rest of the information doesn't have to refresh.
Some more code here:
<?php
if($guessess_open == 1){
echo "<h2>guesses are CLOSED</h2>";
}
else{
//it's this part that I want to have always shown on the page, but when I visit the page fresh it doesn't show me any results.
<div id="results"></div>
<form name="myform" id="myform" action="" method="POST">
<!-- The all important guess field -->
<label for="guess" id="guess_label">Guess<br></label>
<input type="text" name="guess" id="guess" size="10" value=""/>
<?php
echo "<input type='hidden' name='user_id' id='user_id' size='10' value='$user'/>";
echo "<input type='hidden' name='item_id' id='item_id' size='10' value='$itemID'/>";
echo "<input type='hidden' name='title' id='title' size='100' value='$title2'/>";
echo "<input type='hidden' name='owner_id' id='owner_id' size='10' value='$ownerid'/>";
echo "<input type='hidden' name='guesses_open' id='guesses_open' size='10' value='$guesses_open'/>";
echo "<input type='hidden' name='exist' id='exist' size='10' value='$exist'/>";
?>
<!-- The Submit button -->
<br>
<input type="image" name="submit" src="URL" width="150px" height="100px">
</form>
The only way I can get it to show something in that part of the page is to have some code in which checks a database for a guess and if it exists display the result, but then when I submit a new form, because it only updates the results it doesn't update the part of code for example could include
else{
include("URL.php?item_id=" . $itemID. "&user=" .$user. "");?>
//it's this part that I want to have always shown on the page, but when I visit the page fresh it doesn't show me any results.
<div id="results"></div>
which shows the latest guess when a user visits the page, but then when a new guess is made, this doesn't update... I don't have to have my forms php up display the result if the above part of code would update each time I make a guess... I hope this is clearer?

Problem with MySQL update [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
table updates empty spaces when user do not enter anything to the textbox
greetings :)
i am having problems updating my database whenever the user clicks on the submit button.
i am going to show you the flow of my program,i already tried figuring out the problem,but i just can't find solutions. i hope someone could help me.
i have 2 problems encountered here:
my database won't update after clicking the submit button
the user may choose which to update,if the textbox is empty,it will update the data with empty spaces.and i want the data to remain as it is if the textbox is empty.
in my program,if you want to update the employee information,you must click the name that contains a link in the page. (in my program its the employee name that needs to be clicked) when clicked,a pop up will open.
the link in my index.php contains the following code:
<td class="sub" width="100" align="center">
<a href="" onclick = javascript:newPopup('empinfo.php?emp=<?php echo $eid ?>');><?php echo$ename?></a>
</td>
NOTE the empinfo.php is my pop up window,it calls the pop up when clicked. emp isthe name i assign to pass in the empinfo.php it contains the employee ID. NO PROBLEM HERE,I JUST WANT TO SHOW YOU THE FLOW
when the empinfo.php appears,it will show this format:
Employee name: //textbox here
Position: /textbox here
Department: /textbox here
Employee Tag: /textbox here
**SUBMIT BUTTON**
when the user clicks the submit button, it should have updated the database with the inputted values,but mine won't update :(
here is the codes i used:
<?php
$con=mysql_connect('localhost','root','mariel') or die(mysql_error());
mysql_select_db('intranet',$con);
if(isset($_POST['submitted']))
{
$qry = "UPDATE gpl_employees_list SET emp_nme = '".$_POST['name']."', emp_pos = '".$_POST['pos']."', emp_dep = '".$_POST['dep']."', emp_tag = '".$_POST['tag']."' WHERE emp_id = '".$_GET['emp']."' ";
mysql_query($qry) or die (mysql_error());
}
?>
this is the content code in my form,together with the submit that i used:
<form action="index.php" method="POST">
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />
<fieldset>
<div class='container'>
<label for='ename' >Employee name:</label><br/>
<input type='text' name='ename' id='ename' value='' maxlength="50" /><br/><br/>
</div>
<div class='container'>
<label for='pos' >Position:</label><br/>
<input type='text' name='pos' id='pos' value='' maxlength="50" /><br/><br/>
</div>
<div class='container'>
<label for='dep' >Department/Division:</label><br/>
<input type='text' name='dep' id='dep' value='' maxlength="100" /><br/><br/>
</div>
<div class='container'>
<label for='tag' >Employee Tag:</label><br/>
<select name="tag" id="tag">
<option value="Y">Yes</option>
<option value="N">No</option>
</select> <br/><br/>
</div>
<div class='container'>
<input type='submit' name='Submit' value='Submit' onclick = "location.reload();window.close()"/>
</div>
</fieldset>
</form>
i hope someone could clear it up for me
MisaChan
It's not updating because you probably need to refer to $_POST['eid'] instead of $_GET['emp'] because you don't have it in index.php like index.php?emp=1. You already have that field so use that:
<input type='hidden' name='eid' id='eid' value= '<?php echo $_GET['emp']?>' />
Also you don't need to do this:
onclick = "location.reload();window.close()"
Type submit reloads the page by default.
Lastly, consider #Sam152's pointers :)
There could be a number of things wrong, but these points should help you debug your script.
Firstly you need to escape your post
variables to ensure things like
apostrophes don't mess up your query,
it's also a security vulnerability.
Secondly, make sure your form action is pointing to the PHP script. Maybe put a print statement at the top of the script to make sure PHP is actually receiving the data.
Then assign the value of the SQL query to a variable and print it out before you run it. You can then easily see what's being sent to the SQL server. Maybe run it in an SQL management tool my phpMyAdmin and observe any errors with it.
Hope this helps. Feel free to update your question with new information as it comes.

Categories