Saving user input to a page - php

I feel i am missing a crucial link in my design and i was hoping some fine programmer could fill me in (this is not really my field of experience)
What I have
page - form.html
<div style="text-align:left">
<form action="addtoserver.php" method="post">
Server Name: <input type="text" name="servername"><br>
<br>
Server Address: <input type="text" name="serveraddress"><br>
<br>
Port Number: <input type="text" name="portnumber"><br>
<br>
Server Description :<TEXTAREA NAME="description" ROWS=3 COLS=30 ></TEXTAREA>
<br>
<input name="Submit" type="submit" value="Add Server" />
</form>
<div style="text-align:left">
page - addtoserver.php
<html>
<body>
Server Name :<?php echo $_POST["servername"]; ?><br>
<br>
Server Address : <?php echo $_POST["serveraddress"]; ?><br>
<br>
Port Number : <?php echo $_POST["portnumber"]; ?><br>
<br>
Server Description : <?php echo $_POST["description"]; ?><br>
<?php
$ip = $_POST['serveraddress'];
$port = $_POST['portnumber'];
if (!$socket = #fsockopen($ip, $port, $errno, $errstr, 30))
{
echo "<centre><font color='red'><strong>Server Is Offline!</strong></font></center>";
}
else
{
echo "<centre><font color='green'><strong>Server Is Online!</strong></font></centre>";
fclose($socket);
}
?>
</body>
</html>
Everything is all gravy
Purpose
I designed this for the purpose of my website a user will click a link "Add server" and will be directed to the form page, they will fill out the form page and once submitted be directed to the addserver.php page.
On this page i wont the post to pertinently stay there and the next user to to come in and add his server and for that post to be listed underneath.
My Dilemma
I am not sure what the next step is to take, would i have to add the users input to Mysql and then export it from Mysql to the page or is there another way. would i have to rite a PHP script in order to get the posts to come in one after the other if imported from the data base.

I am not sure what the next step is to take, would i have to add the users input to Mysql and then export it from Mysql to the page?
If you require persistent data then you'll sure need some static storage such as a database or a XML file. In both way, yes you want to store the user input into it (make sure you fully understand the security need behind such an action) and then retrieve it from there.

Related

Issue with html form and php using Xammp

I have installed XAMPP to simulate a php server environment on my pc. Unfortunately my html form will do nothing when I hit submit. Here is html:
<html>
<form>
<form action="http://localhost/New/nn.php" method="post">
Why don't they play poker in the jungle?<br>
<input type="radio" name="jungle" value="treefrog"> Too many tree
frogs.<br>
<input type="radio" name="jungle" value="cheetah"> Too many cheetahs.
<br>
<input type="radio" name="jungle" value="river"> Too many rivers.<br>
<br>
<input type="submit" name="submit" value="Submit"><br>
</form>
</html>
Here is php:
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['jungle'])) {
if ($_POST['jungle']=="cheetah") {echo "You got the right
answer";}
if($_POST['jungle']=="treefrog"){echo "You selected treefrog but
answer is cheetah\n";}
if($_POST['jungle']=="river"){echo "You selected river but
answer is cheetah\n";}
}
else { echo "You did not choose an answer for jungle.\n"; }
}
else { echo "Please submit the form."; }
?>
In the code provided, you do have two form opening tags:
<form>
<form action="http://localhost/New/nn.php" method="post">
That might be a source of your problem.
On chome for mac (latest version).
Right click on your page, inspect
Go to the Network tab and reload
Click on your file name (for me test.html) and go to the preview tab. See how you have both tags
Now back on the Elements tab, see how you have only the first one, hence sending you back to the same page (that is the default action)

Why php code executes 3 times in wordpress

I am very new to wordpress. I am trying to do the following:
a. I have a very simple html and php based website. I have a signup page which accepts user id and password and then on click of submit button it calls a php file which verifies the userid/password and saves it in the database. THIS WORKS PERFECTLY FINE
b. Now, I trying to move those two pages to wordpress platform. I have installed a php plugin also. Now the problem in wordpress platform is whenever I hit the submit button, in the database 3 duplicate records are getting appended. I have been trying a lot but could not fix this
c. I tried different php plugins but does not work
d. After reading few help documents on the web, I have tried creating my own template page, even that does not work
Any help in this regard will be much appreciated.
EDIT
Form:
<form action="http://xxxxxx.xxx/xxxxxx" method="post">
Enter User ID:
<input name="userid" type="text" value="" />Password:
<input name="password" type="text" value="" />
Confirm Password:
<input name="confrimpassword" type="text" value="" />
<input type="submit" value="SIGN UP" />
</form>
Below is the php file:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
//echo "Favorite color is ".$_SESSION["favcolor"].".<br>";
//echo "Favorite animal is ".$_SESSION["favanimal"].".";
?>
<?php
mysql_connect("localhost","***********","**************");
mysql_select_db("******");
$sql1=mysql_query("select * from login_details where customer_id='$_POST[userid]'");
$row=mysql_fetch_assoc($sql1);
if (!$sql1) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
//exit;
}
else {
echo " inside last else";
$sql2=mysql_query("INSERT INTO login_details VALUES ('$_POST[userid]','$_POST[password]')");
echo "Account successfully Created...";
}
mysql_close();
?>
</body>
</HTML>

Limit user to submit the form

I have a HTML form where data is filled and submitted.Now i want to limit the users to hit submit i.e i want only set of users whom i want to hit submit,if any other person hits the submit a message should display as "You are not authorized to submit".
Example form is :
<html>
<?php echo ( !empty($_SESSION ['user']) ) ? $_SESSION ['user'] : 'USER'; ?>
<body>
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="firstname">
<br>
Last name:<br>
<input type="text" name="lastname" value="lastname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here i would get the usernames from :
<?php echo ( !empty($_SESSION ['user']) ) ? $_SESSION ['user'] : 'USER'; ?>
This "user" am collecting from the login page.Who ever logs in with their credentials their user name will be passed through the pages for the sake of session.
Here "user" i want only particular users to be able to submit.
Thanks in advance.
Try this :
EDIT :
Since the OP wants to show button to only MANAGERS, we'll write a query to get those users.
$sel='Select `usergroup` from table_name where user_id='.$_SESSION['user'];
$res=mysqli_query($con,$sel);
$row=mysqli_fetch_assoc($res);
?>
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="firstname">
<br>
Last name:<br>
<input type="text" name="lastname" value="lastname">
<br><br>
<?php
// did changes here
if($row['usergroup']=='manager') // those users who are manager
{
?>
<input type="submit" name="submit" value="Submit">
<?php
} ?>
</form>
</body>
</html>
you need a database table where user specific permission store . After getting login user from session then write a select query if the user has this permission then show the form otherwise hide the form and display message
... and remove the form-node for users that are not allowed to submit, since you can trigger the submit by pressing enter for example.
Basically sepearte the whole thing into two templates:
one with the form for the users u want to be able to submit,
and one without any form-elements (use regular nodes instead: divs, spans, ...) for the ones u only want to show data.
because it doesn't make sense to have/change an input-field if it does nothing.

Copying contents from iframe into input

Hi new to coding and scripting, here is what I have:
<html>
<body>
<p align="left"><iframe id="aa" style="HEIGHT: 25px; WIDTH: 227px"src="http://www.html-assets.com/assets/html-building-blocks/php-display-user-ip-address.php" frameborder="0" width="600" name="aa" scrolling="no"></iframe></p>
</br><input type="text" id="ee">
<input type="checkbox" name="dd" value=" onclick="myFunction()"><em>Check the box to copy the IP Address above</em>
<script>
myFunction() {
var x = document.getElementById("aa");
var y =(x.contentWindow || x.contentDocument);
if(dd.oncheck==true){
f.ee.value = y;
}
}
</script>
</body>
</html>
What I am trying to do is have something where the page lists out the IP Address of an end-user. I want the user to have the ability to copy this address into an input field that can be added to a form that can be submitted.
How would I script this? At this point, the object in my iframe is outside my domain; is this impossible to call because of the same origin policy? I am planning on creating this using a .php within our own domain; I will adjust the code accordingly.
Yes, you will have to use a .php file for this.
Once you have created your php file, then insert this code:
<?php
$userip = $_SERVER['REMOTE_ADDR'];
?>
<input type="text" value="" id="textbox1"/>
<input type="button" value="Click me" onClick="document.getElementById('textbox1').value = '<?php echo $userip;?>' "/>
The user's ip will be saved in the variable $userip. Then, when the button is pressed, the textbox textbox1 will be filled with the user's ip address.
I hope this helps!

display an alert after a user clicks a submit,and comes back after processing in another php page

How can I display an alert or a message after a user clicks a submit button in a form?
I want the alert to show when the page has loaded after the submit button has been clicked.
When submit form is clicked its goes to another php page updated the db and and redirected to the form page itself. I am doing it in PHP. Is there anyway I can post data, check in the form page and display a hidden message?
header('Location: http://www.xxxxxx.co.uk/NottTest/editqr.php');
This is the redirect link after updating the DB, can I parse a message here?
This is the page where the db is updated ..
<body>
<p>
<?php $sltdtpst= $_POST["qrtopic"]; ?>
<?php $selctsm= $_POST["qrsubmenu"]; ?>
<?php $selcttxt= $_POST["text12"]; ?>
<?php
$link = mysql_connect('www.xxxxx.co.uk', 'xxxxxx', 'xxxx');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("web3fffasd", $link);
$update = "UPDATE qr_table SET $selctsm = '$selcttxt' WHERE id_qr= $sltdtpst";
mysql_query($update, $link);
mysql_close();
header('Location: http://www.xxxxxx.co.uk/NottTest/editqr.php');
?>
</p>
</body>
So after updating the database a hidden should be displayed here in the form page
<form name="updateqrnew" form action="edider1.php" onsubmit="return validateForm()" method="post">
<div class="form_description">
Update Contents for QR below
</div>
<ul >
<li id="li_2" >
<label class="description" for="element_2">Quick refresher topics </label>
<div>
<select class="element select medium" id="qrtopic" name="qrtopic">
<option value="" selected="selected"></option>
<option value="1">Anxiety</option>
</select>
</div>
</li> <li id="li_3" >
<label class="description" for="element_3">Submenu </label>
<div>
<select class="element select medium" id="qrsubmenu" name="qrsubmenu">
<option value="" selected="selected"></option>
<option value="start_of">Starting off</option>
</select>
</div>
</li> <li id="li_4" >
<label class="description" for="element_4">Enter the article below</label>
<div>
<textarea id="text12" name="text12" class="element textarea medium"></textarea>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="431595" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Update" />
</li>
</ul>
</form>
Let me see if I understand this
The user clicks on a submit button,
the action script processes it, then
does a redirect to a new script,
and you want to inform the user that their update worked, is that it?
In this case, what you may want to look at is passing a result code from the action script to the redirect script. Then the redirect script can lookup the result code passed in a emit the proper message to the user. (Does not have to be a pop-up, necessarily in this scenario.) Passing the result code can be done in a few ways, including using the query string, or using a cookie, or using sessions. The first is easy, but makes for a slightly more messy URL. The second is rather deprecated. The third is the more generally accepted way of doing this, but if this is the only place you'd need a session, is probably overkill for your application.
Using the query string, your redirect may look like this:
header('Location: http://www.xxxxxx.co.uk/NottTest/editqr.php?r=1');
where 1 can be a code for sucess, for example, or you could have other codes if there was an error you wanted to display, etc. If you need more information than just a code, you can pass that on the query string as well. Note that this does present some application security issues, so you have to ensure that your code will do the right thing if someone simply calls the redirect script directly with a query string that means something has happened in your application. That is why it's not the best way to do this.

Categories