How can I disable someone refreshing a page? - refresh

I just made a php code to add name,email,etc to be added into a table and when I click refresh, the same rows of information will appear and I don't want that to happen if the user will refresh the page...
ok, I have this page1.php where you can enter info and it will redirect you to page2.php but when I click F5 it enters the same info again
(I am just idiot-proofing it - what if someone wants to refresh the page or accidentally refreshes it, right?)
page1.php:
<html>
<body>
<form action="page2.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Email: <input type="text" name="email" />
<input type="submit" />
</form>
</body>
</html>
page2.php:
$sqlinsert="INSERT INTO sampletable (FirstName, LastName, Email)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]')";
if ($conn-> multi_query($sqlinsert) === TRUE) {
echo "New record created successfully.<br>";
} else {
echo "Error: " . $sqlinsert . "<br>" . $conn->error;
}
$conn->close();

Related

Getting response from python to browser (CGI python)

I am trying to get(reflect) corresponding radio button as soon as I choose the option from dropdown menu.
I am unable to get the answer, I am new to python. could you guys please help , below is my code :
HTML CODE:
<!DOCTYPE html>
<html>
<head>
<title>
Program
</title>
</head
<body>
<h3>
first program :)
</h3>
<form action="/cgi-bin/mds/dropdown.py" method="post">
<br>
<select name="dropdown" >
<option value="MCR" selected> MCR </option>
<option value="OCS" > OCS </option>
</select>
<input type="radio" name="file" value="raw"> raw <br />
<input type="radio" name="file" value="std" /> std <br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
My expectation when I select any of the OCS/MCR dropdown, the radio button should dynamically generate with raw/std name.
dropdown.py file code:
#!/usr/bin/python
import cgi
import cgitb
form = cgi.FieldStorage()
if form.getvalue('dropdown'):
menu = form.getvalue('dropdown')
else :
menu = "not entered"
print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<title>Hello - Second CGI Program</title>")
print("</head>")
print("<body>")
print("<h2> %s </h2>" % (menu))
print("</body>")
print("</html>")
If i well understand you need to do that using ajax (jquery) in your html file, it sends request asynchronously to your python file that handles it as what you clicked on.
maybe this link helps you
jQuery get value of select onChange

Regex and POST in same connection

please bear with me, i'm brand new to Python!
I'm trying to login to a website which uses PHP. The form contains two hidden fields, the value of one and the name of another are generated on page load.
My code below successfuly accesses the page and using regex manages to return the values - great!
The problem I am having is that I then generate my querystring that will be used for the POST (this contains the two values obtained earlier) and opens the url again. This generates brand new tokens/values and my originals are of no use.
Can someone shed some light on how I can connect to a site, use regex to get the values and then POST all in the same connection.
I hope i've made myself clear, if not please let me know.
Thanks in advance for your help.
import urllib2,urllib,re,cookielib
url='http://www.example.com/index.php'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
token1=re.compile('<input type="hidden" name="return" value="(.+?)" />').findall(link)
token2=re.compile('<input type="hidden" name="(.+?)" value="1" />').findall(link)
print token1[0]
print token2[0]
username = 'username'
password = 'password'
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password, 'return' : token1[0], token2[0] : '1', 'Submit' : 'Log in', 'option' : 'com_users', 'task' : 'user.login'})
opener.open('http://www.example.com/index.php', login_data)
resp = opener.open('http://www.example.com/index.php')
FORM:
<form action="/index.php/welcome2" method="post" id="login-form" >
<fieldset class="userdata">
<p id="form-login-username">
<label for="modlgn-username">User Name</label>
<input id="modlgn-username" type="text" name="username" class="inputbox" size="18" />
</p>
<p id="form-login-password">
<label for="modlgn-passwd">Password</label>
<input id="modlgn-passwd" type="password" name="password" class="inputbox" size="18" />
</p>
<p id="form-login-remember">
<label for="modlgn-remember">Remember Me</label>
<input id="modlgn-remember" type="checkbox" name="remember" class="inputbox" value="yes"/>
</p>
<input type="submit" name="Submit" class="button" value="Log in" />
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="user.login" />
<input type="hidden" name="return" value="aW5kZXgucGhwP0l0ZW1pZD0xMjc=" />
<input type="hidden" name="c813c34837e4e48e8e3268c0a42912a2" value="1" />
</fieldset>
<ul>
<li>
<a href="/index.php/my-account/my-details?view=reset">
Forgot your password?</a>
</li>
<li>
<a href="/index.php/my-account/my-details?view=remind">
Forgot your username?</a>
</li>
<li>
<a href="/index.php/register">
Create an account</a>
</li>
</ul>
</form>
When you write...
opener.open('http://www.example.com/index.php', login_data)
resp = opener.open('http://www.example.com/index.php')
Why not just this?
resp = opener.open('http://www.example.com/index.php', login_data)
I've never used this Python library, but my first reaction is that this would give you the response text all in one request, with which you can get the new token, wouldn't it?
Update based on form: It looks like your problem is you're POSTing the login info to index.php rather than index.php/welcome.

radio buttons and cookie settings

I don't know anything about cookies or how to set them and I need some advice. I have two radio buttons. For example if an option is changed from one to another, that option will remain even if the page is refreshed or changed on other pages where this radio buttons exist, and I need to make the cookie setting for this code. Can someone can give me some advice regarding what code should I add to my php?
This is js code:
$(document).ready(function() {
$('radio[name=radio]').each(function() {
$(this).click(function() {
my_function();
});
});
});
my_function()
{
var value_checked = $("input[name='radio']:checked").val();
$.ajax({
type: 'POST',
url: 'page.php',
data: {'value_checked':value_checked},
});
}
html code
<form>
<div id="radio">
<input type="radio" id="radio1" name="radio" checked="checked" /><label for="radio1">Choice 1</label>
<input type="radio" id="radio2" name="radio" /><label for="radio2">Choice 2</label>
</div>
</form>
It is important to remember that cookies can only be set before any output is sent to the client on a webpage, because cookies are set as headers, and headers can only be sent prior to any part of the webpage output. Therefore, you need to refresh the page to set a cookie to the value of your radio button.
At the very top of the php, BEFORE the <!DOCTYPE> html or <html> tag, you need to add something like this:
<?php
if(isset($_POST['radio1'])) {
setcookie('radio1', true, 600, '/');
setcookie('radio2', false, 600, '/');
} else if(isset($_POST['radio2'])) {
setcookie('radio2', true, 600, '/');
setcookie('radio1', false, 600, '/');
}
?>
The above code will make sure that only one of the cookies is set to true and the other is set to false. The cookies will expire after ten minutes.
This is after you properly set up the html form so that you can detect that your user has selected a button:
<form method="POST" action="index.php">
<div id="radio">
<input type="radio" id="radio1" name="radio" checked="checked" />
<label for="radio1">Choice 1</label>
<input type="radio" id="radio2" name="radio" />
<label for="radio2">Choice 2</label>
</div>
</form>
The PHP Manual page has more information: http://php.net/manual/en/function.setcookie.php
EDIT: Semantic code changes and fixed the html tags described.
See setcookie examples how to set cookies in PHP. But you can do it also with javascript js_cookies.

Invoking a CFC from a FORM

Like the title says, I am invoking a CFC from a FORM. This is how I am doing it now, but there has to be a better way.
Form:
<cfform action="choice.cfm" method="post">
<cfinput type="hidden" name="method" value="DeleteMessage">
<cfinput type="hidden" name="announcement" value="#announcement#">
<cfinput type="hidden" name="ID" value="#ID#">
<cfinput type="submit" value="Delete" name="form.OKbutton1" onclick="return confirm('Are you sure you want to delete?');">
</cfform>
Action page:
<cfif IsDefined("form.OKbutton1")>
<cfinvoke component="pdprojects.scr.changedisablesysequipment"
method="DeleteMessage"
returnvariable="DeleteMessages"
argumentCollection="#Form#" />
</cfif>
I figured out a way to streamline this with jquery and cfc. Page updates fast, and no worries if the person hits back button or refreshes the page. You could probably do a lot more with JQuery to append a div tag instead of location.reload(); (Page Refresh) at the end of the call.
CF Call
<cfajaxproxy cfc="pdprojects.scr.changedisablesysequipment" jsclassname="choice_cls">
Here is the jquery
<script src="/js/jquery-1.7.1.min.js">
<script>
$(document).ready(function(){
$("#OKbutton").click(function(){
if ($('#Announcement').val() == "") {
alert("Please enter message in the text area!");
}
else {
var instance = new choice_cls();
instance.setForm("enterit");
instance.InsertMessage();
location.reload();
}
});
$("#OKbutton1").click(function(){
var instance = new choice_cls();
instance.setForm("deleteit");
instance.DeleteMessage();
location.reload();
});
});
Here is my html
<cfform action="" id="enterit" name="enterit" method="post">
<cfinput type="hidden" value="#session.username#" name="thisname">
<cftextarea cols="50" rows="8" name="Announcement"></cftextarea><br>
<cfinput type="button" value="Enter Announcement" name="OKbutton" id="OKbutton">
</cfform>
<cfform action="" id="deleteit" name="deleteit" method="post">
<cfinput type="hidden" name="method" value="DeleteMessage">
<cfinput type="hidden" name="announcement" value="#announcement#">
<cfinput type="hidden" name="ID" value="#ID#">
<cfinput type="button" value="Delete" name="OKbutton1" onclick="return confirm('Are you sure you want to delete?');">
</cfform>
Then just create your CFC for coldfusion!

Preserving input value of type="file" in ColdFusion on postback

I have a form in ColdFusion that initially has 5 input fields for file uploading. Should the user realize they have more than 5 files to upload in the process of attaching them, I would like the form to preserve the values when it submits itself for the change in # of fields.
Using the <cfform> tag with the preservedata="yes" attribute is supposed to accomplish this - but all I'm getting is a temp value stored in the input's value on resubmit that isn't displayed in the field nor works for a submission.
edit: Thanks for the great answers everyone, you all helped and were correct. I was able to implement Adam's suggested solution. Works great! Thanks!
function changeFieldCount() // javascript function for submit on input count change
{
var count = document.forms[0].numtotal.options[document.forms[0].numtotal.selectedIndex].value;
document.forms[0].action = "me.cfm?count=" + count;
document.forms[0].submit();
}
<cfparam name="url.count" default="5">
<cfform name="dispfiles" method="post" enctype="multipart/form-data" preservedata="yes">
<!--- looping for file input fields --->
<cfloop index="counter" from="1" to="#url.count#">
<cfinput type="file" name="file#counter#" size="50" maxlength="60"><br>
</cfloop>
<br>Number of Files to Attach:
<!--- looping for # of input selection--->
<cfselect name="numtotal">
<cfloop index="cnt" from="1" to="20" step="1">
<cfoutput>
<option value="#cnt#" <cfif #cnt# eq #url.count#>selected</cfif>>
#cnt#
</option>
</cfoutput>
</cfloop>
</cfselect>
<cfinput type="button" name="op-display" value="Display" onclick="changeFieldCount()">
<cfinput type="button" name="op-upload" value="Attach Files" onclick="submitfrm(this.form)">
<cfinput type="button" name="cancel" value=" Cancel " onclick="window.close()">
</cfform>
This is what I'm getting when I view source on the resulting submission:
<input name="file1" id="file1" type="file" value=".../cfusion.war/neotmp8858718543274653167.tmp" maxlength="60" size="50" /><br>
This is by design in all browsers for security reasons. There is no way you can insert the value for a file field.
To elaborate on #SpliFF's answer, what you need to do is dynamically create more file fields with JavaScript.
Here's an example that uses jQuery to make the JavaScript a little easier. I've also used a variable to track the number of file fields displayed so that they all have a unique number appended to their names, making it possible to loop over and uniquely identify them server-side.
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//track the current number of file fields displayed
var numUploadFields = 5;
//attach an anonymous function to the button to add more fields
$(function(){
$("#add5").click(function(){
for (var i = 0; i < 5; i++){
numUploadFields += 1;
var newHTML = "<br/><input type='file' name='upload" +
numUploadFields + "' />";
$("#someSection").append(newHTML);
}
});
});
</script>
<form method="post" action="">
<div id="someSection">
<input type="file" name="upload1" /><br/>
<input type="file" name="upload2" /><br/>
<input type="file" name="upload3" /><br/>
<input type="file" name="upload4" /><br/>
<input type="file" name="upload5" />
</div>
<input type="button" id="add5" value="Add 5 more file fields" />
</form>
Build additional file inputs using JS DOM methods. Since you aren't leaving the page this is fast and nothing is lost.