I have a registration form wherein one of the field is username which needs to be unique. So, I am checking it first in my database before insert proceeds. If username exists, I put the below line. My problem is after clicking OK, it refreshes the page and clears all the inputted data of the user. I know I can use return false but how do I put this on my echo?
else if(mysql_num_rows($result)>0)
{
echo '<script>alert("Username is not available")</script>';
}
else
{
insert goes here
<form id="appform" action="register.php" method="post" onsubmit="return validateform('appform');">
<input type="text" class="textarea1" name="stdname" size="30" onkeyup="isallnospace(this)" /> .
}
You are validating in the worst way possible. If you want to validate using both client side and server side script, please use ajax.
Uses of Ajax:
Update a web page without reloading the page
Request data from a server - after the page has loaded
Receive data from a server - after the page has loaded
Send data to a server - in the background
For more details, visit AJAX Tutorial
best way is to check with ajax before submit, but if you want you can add the $_POST values to the form when it returns to it like so
<input type="text" name="username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];}?>" />
Related
My code looks likes this:
<form action="register.php" method="post">
<input type="text" name="uname">
<input type="submit" >
</form>
In register.php file I have a database connection codes.
I need to load another html page after submitting the form simultaneously if the database is connected through the register.php file.
How to do that?
Simply redirect the user to the second page once the registration is successful, through means of the header() function:
index.php:
<form action="register.php" method="post">
<input type="text" name="uname">
<input type="submit" >
</form>
register.php:
// Process registration
if (valid) {
header('success.php);
}
success.php:
// Thank the user for registering
Alternatively, you could always simply process your success page on register.php itself.
Hope this helps! :)
Welcome to SO. It sounds to me like you don't actually need to load 2 URLs simultaneously and what you are looking for is an if statement to execute some code only if post data is set
if(!empty($_POST)){
//code to execute when form is submitted
}
else {
//code to execute otherwise
}
If you do need to make a request to 2 URLs at the same time you are looking for an AJAX request.
More detail in your question about your setup and end goal would be useful to help further.
in your Register.Php
add something like this
check your Sql result something like this
$sql='select * Tablename where condition'
$result= fetch($sql)
$row=fetch_array($result)asdsad
if($row>0){
header(Location:'connected.php') // whatever your html
}
else {header(Location:"failed.php")//whenever query got no row
}
I'm not expert in PHP, and I'm trying to create public chatroom for my simple website.
I'm using SQL database to store messages.The file named chat_index.php getting all messages from database and show it to users. Also it has a simple form to send message with PHP GET method. The following is code for my form.
<form method="get" action="sendmessage.php">
<input name="msg" placeholder="Message" data-theme="a" type="text">
<button type="submit" class="ui-btn ui-corner-all">Send</button>
</form>
With above code I'm sending data to sendmessage.php file. In this file adding message to database and user redirect to chat_index.php with this code.
header("Location: chat_index.php");
exit();
After redirect page loading correctly on browser window. But URL end like this
...../sendmessage.php?msg=test_message
So if I reload the page message sending again and url getting correct like this
...../chat_index.php
How can I resolve this problem?
UPDATED
I tried with POST method. but not solved. browser showing content in chat_index.php and url ending with ../sendmessage.php
Why you are recieveing the url variables is because you are using GET. use POST instead and the variables will be gone.
example:
<form method="POST" action="sendmessage.php">
If you don't want to send data to the query string use POST instead of GET.
<form method="POST" action="sendmessage.php">
This should clearly be a POST form. Any form saving or changing anything on the server should be. Otherwise (using get) people could add messages by just following a link, which is not intended. The redirect after save is OK and good practice. Note that such redirects only work before any output is produced.
Cannot say much more by what you have posted. You should also check if your saving and redirect code is triggered correctly when message is posted.
This is what is happening, the form is being submitted from chat_index.php, so the action ends up being chat_index.php/sendmessage.php.
To solve this change the method to post as directed and change your action to /sendmessage.php
your form should look look like this
<form method="POST" action="/sendmessage.php">
....
</form
hope this help
If this is to be a simple forum site, then it would be much less complicated to not redirect at all. Just set your action attribute to "", and when someone submits the form, it will post the data back to the page. So do something like this:
<form method="post" action="">
<input name="msg" placeholder="Message" data-theme="a" type="text">
<input name="function" type="hidden" value="post_message">
<button type="submit" class="ui-btn ui-corner-all">Send</button>
</form>
Then put the php code that was in sendmessage.php into chat_index.php above the starting DOCTYPE and html tags, and delete sendmessage.php. Or alternatively, use include('sendmessage.php') in the same spot. Then when the page loads, check if $_POST['function'] == 'post_message', and if this condition is true, then execute the stuff you had in sendmessage.php. That will be much more compact, and the user will be redirected once instead of twice. Also, I don't know your file structure, but you might want to rename chat_index.php to just index.php to make it intuitive and so that people can't see inside your directory.
I have three jsp pages and a spring controller.LoginForm.jsp,userAccount.jsp and AccountSummary.jsp.LoginForm will send data to userAccount which has an iframe.userAccount has links in it.Onclicking on one of the link AccountSummary page is displayed in the iframe of userAccount.
Now i need to pass the form data that is sent to the parent to the controller again if Accountsummary link is clicked.Then AccountSummary page is displayed in the iframe.Here the controller will validate the values and send appropriate data to account summary which will then be displayed
I tried adding the below code:
userAccount.jsp
Account Summary
<form action="/MyBankProject/AccountSummary" method="post" target="frame1" id="myForm">
<input type="hidden" name="userName" value="${userName}"/>
<input type="hidden" name="password" value="${password}"/>
</form>
<script>
function myfunc() {
document.getElementById("myForm").submit();
}
</script>
In the above code i am trying to take the form values sent to the parent, userName and password .And making them hidden and submitting so that controller will receive those values.But in vain the values are not passed to the controller.i couldn't figure out where i made a mistake.
I am new to javascript .I feel i explained the problem i am facing.kindly help me and let me know if i missed in conveying the problem.
Is there anything i missed.
I have an online game site, that submits users' records and displays them in site score board.
But, some users like to be #1 (!) and they create a form with my "action" and "method" forms that are for submitting.
Here is my form:
<form action="submit.php" method="post" id="form">
<input type="hidden" id="time" name="time">
<input type="hidden" id="name" name="name" value="<?php echo $_SESSION['name']; ?>">
</form>
And here is js that runs at the end of the game:
document.getElementById("time").value=finaltime;
document.getElementById("form").submit();
"submit.php" inserts $_POST['time'] and $_POST['name'] into mysql database.
So, How can I prevent users to submit their own created form?
In another meaning, how can I find the origin of the submitted form in "submit.php" ?
As far I as I am aware, there's no way to tell from which domain a record has been inserted into a database, unless the webpage passes that information when inserting. However, people aren't able to just insert into a database, if that's your concern. when they connect to the database they should require a username and password first (assuming you have that set-up). Here's how PHP connects to a database:
mysqli_connect("domain.com","username_here","password_here","database_here");
Even then, they still need to provide which table to insert the information into.
I am trying to submit a form using get method but I dont want the form to refresh or rather when it reloads I want to maintain the data. There seems to be lot of questions on similar lines but none that I tried helped. I am posting my code here:
<script type="text/javascript">
function formSubmit()
{
document.getElementById('form1').submit();
return false;
}
</script>
<form id = "form1" method = "GET">
<br> Query: <input name="query" id="query" type="text" size="50" value="">
<input type="button" name="search" value="Get News" onclick = "formSubmit()">
</form>
I am using python on the server side.
Thanks
The statement:
I am trying to submit a form using get method but I dont want the form to
refresh or rather when it reloads I want to maintain the data.
Implies to me that your end goal requires AJAX or at least some passing of data to the server and back. You will not be able to retain scope within Javascript over a page refresh without the use of something like cookies or passing data to/from the server. Having said that these are more akin to secondary storage mechanisms while you want to retain scope (or primary storage). To do this I would recommend AJAX.