Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm wondering how to differentiate different submit and forms to a certain php function.
Currently I have 2 forms in my page, but each submit button will do 2 different thing.
I've tried using ISSET to control the submit but if failed, it always refer back to the same function.
Initially what I want to do is I wanna have the user to key in some verification info and submit the info to the database to do some checking (the data is in the database) and update the result on the same page, then only they proceed to submit the whole updated form to the payment gateway.
Assign a name to your submit button like
<input type="submit" value="Update" name="first_form" />
<input type="submit" value="Update 2" name="second_form" />
So, now you can execute a particular code like
if(isset($_POST['first_form'])) {
//Process first form
}
if(isset($_POST['second_form'])) {
//Process Second Form
}
I just read your question again, not getting much, but it seems like you want to carry values to another form or you want to show forms only if the first form is completed, so the best way to do this is to have a session var, which will hold the users form data, so that you can carry it on another page, also you can set flags from which you can show particular data to the user, for example, if user completes form 1 set $_SESSION['completion'] = 1 so you can use a condition to check whether session var isset, and if it is, whats the value and show the content to the user accordingly.
Due to the fact that PHP is a server side language, it only runs when the page loads. If you want information on the screen to change without reloading the page, you will probably need to use Javascript or Ajax.
if you use 2 form on basic html page, that wont do, because once it submitted, the page reloaded..
ajax is the aswers..
var dataSet={ SERIALIZE_VALUE FROM FORM1 };
$.post(URL_TO_PHP_ACTION_SCRIPT,dataSet,function(data){
// check the return value here if its valid, then enable the 2nd form for submitting
});
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an edit form with the url looking like localhost/edit-form/*code* The code is a random 16 long string. What I would like to do is when submitting the form, refresh the page showing the form again with the new values.
I have tried to redirect with an extra attribute like localhost/edit-form/*code*/message which returns an error saying page not found.
I have also tried something like localhost/edit-form/*code*?message=1 but message isnt available to get via $_GET.
My goal is just to have a div alert saying "form edited" after the page is refreshed.
Flash messages are usually stored in $_SESSION. You could create a custom method that...
Stores the message to the session.
Deletes the message from the session, as soon as it is displayed for the user.
You could then call the method in your template file as soon as the user is redirected to it.
After your operation you can store message is $_SESSION['mesaage']
If this variable is set then you can display the message. As an oddon you can store success and failure class in separate session variable and display with success or failure class.
Once text is displayed you can unset sessions.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've set up a web page to query a database based on a user search that returns database row values into an HTML table via PHP and jQuery. Next to each returned row sits a button that displays 'Add to wish list'. I'd like for this button to be able to add that given row to a table on another web page, and store it there whilst the user navigates and searches the database further, adding more rows to the new table if necessary.
I've looked into this and the best solution I've come up with so far seems to be utilising PHP to store a $session like shopping carts on web pages do. However most of the scripts on the web are far more complex than I'll need for this web page as it's not an e-commerce site.
Is there a simpler way to do this, perhaps in jQuery that on click of 'wishlist' simply appends the data from a row to an HTML table on another page in a similar fashion?
Probably you can use HTML5 WebStorage supported by about 87% of browsers click to see browsers support here http://caniuse.com/namevalue-storage. Otherwise you have to use PHP session or Database or something with Jquery as you said. You can follow this to learn basic about HTML 5 LocalStorage http://diveintohtml5.info/storage.html
Remember this feature only supports strings to save data. Use JSON.stringify() and JSON.parse() to parse and save array if you want. You can use some code like this
//To set user's selected wishlist
var wishlistarray = [];
wishlistarray[0] = 'save data in whatever way you like';
localStorage["wishlist"] = JSON.stringify(wishlistarray);
//To get previously saved user's wishlist
var wishlistarray = JSON.parse(localStorage["wishlist"]);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a simple page that shows 4 different divs, all with a <h1> title (or other tag that shows text) and a form (with 1 input text) and a submit button... so far so good.
My answer is simple: I am looking for a way that:
when the user inserts a text and submit the form, the <h1> title shows the text that the user wrote
the form disappear (or takes style display:none)
this will take effect even if I refresh the page or view page in a different computer (probably need to save the data in a DB)
restart this process from the beginning (even if I need to code again).
This is for a mini game to provide to users 4 different choices and, if the user A select option 1, the <h1> will show text something like "User A was the first to choose this option. Please select a blank option" and, after the 1st raw over (point 4 described above), restart all forms.
NOTE: I am not asking in the way to "please do the code for me". I also searching for a way to store data in <php ?> - finding redbeanphp project.
I am also a newbie in SQL (just started to study SQLi last month to android development). My question is ONLY to looking for the best way to do this and what I need.
This would be the workflow:
When you click a button to submit the form JavaScript will send the form data to the server as an AJAX request and wait for a response from the server.
The PHP code on the server will read the data from the AJAX request and save it in a database and echo a success response along with the text to display.
Your JavaScript will receive this success message and hide form from the DOM and display the text in the header.
If you want the data to persist on the page on reload then you can save a flag in PHP session. Sessions persist in until you close browser window.
4.1 Use another AJAX call remove the flag from the session and reload the content.
So, if you are good with HTML and CSS you need:
JavaScript, AJAX, a JavaScript library to make things easier such as JQuery.
A server side language such as PHP.
A database to persist data. There are many choices. I am going to recommend MySQL just because there so many tutorials for it out there.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I currently have a PHP page called item.php that has a link on it "Add Item".
When a user clicks the link, it takes them to save.php, where the page does some stuff like saving the item to the database. On that page, there is a redirection after the saving code has run:
header("Location: item.php);
Now the item.php page says "Remove Item" for the link, as it should.
Here's the problem: if the user now hits the Back button on their browser, it shows them the item.php page again (which I don't want) and shows them the "Add Item" link again (which I also don't want).
How do I solve this issue so that there is only one item.php page in the browser history stack?
In you're not willing to use AJAX, I think the real question you mean to ask is "How can I refresh item.php when the back button is pressed?"
There are several ways to achieve this, here's one that's done using JavaScript/jQuery. Store a hidden <input> variable called "refresh" and submit it when the user clicks "Add Item":
<input type="hidden" id="refresh" value="no">
Now, you can use the following jQuery function:
$(document).ready(function(e) {
if ($("#refresh").val() == 'yes') { location.reload; } else { $('#refresh').val('yes'); }
});
Here's how this code works: The first time you load the page and call $(document).ready, the hidden input value is "no", which means the aforementioned function will set the value to "yes". Then, when the user hits the "back" button, the hidden field value will remain the same (it will still be yes), but $(document).ready would be called again. This time when it is called, it will reload the page.
Related reading: http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
A solution using PHP: How do I make Firefox reload page when back button is pressed?
Let me know if my explanation wasn't clear or if you have any questions. Hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i'm getting so stuck with my homework. .
how to make tic-tac-toe with php_self?
So, i have 9 button with the number range from 1 to 9. then, when first button is clicked the value will be changed with 'O'. after the player with 'O' symbol had pushed the button, then turn to 'X' symbol appeared if user click the button. the process will be continued until the same symbols appear on horizontal, vertical, or diagonal series.
I hope somebody could help me :(
Thanks
Am not sure if you need to re-event the wheel .. what you need to do is to change the interface ...
There are so many solutions online that can help you with this
PHP & HTML & Javascript
http://code.activestate.com/recipes/276962-php-tic-tac-toe/
http://t-dev.iglu.cz/
http://scripts.franciscocharrua.com/javascript/tic-tac-toe/
http://www.mystcommunity.com/board/index.php?/topic/11367-tic-tac-toe-in-php/
PHP & HTML
http://refactormycode.com/codes/1524-simple-tic-tac-toe-php-pure-html
http://jadendreamer.wordpress.com/2012/01/31/php-tutorial-2-player-tic-tac-toe-game-no-database-required/
Conclusion
If you select any of the script that you are interested in then you can comeback if you are having any difficulty in making it work
Well in the process of answering this question I pretty much did your homework for you. But you should pretend that I have not and I am going to forget about that as well and just focus on what you have asked.
Also, PHP_SELF by itself means nothing. $_SERVER['PHP_SELF'] which is what you are most likely referring to pretty much points to the page itself that you are writing code in. You make it seem as if there is something that is going to help you to make tic-tac-toe but I do not think so. Maybe you can correct me if I am wrong.
You seem to mention that you already have an interface that you are working with. I do not have the code for this interface so I am going to assume that it looks something like this:
http://jsfiddle.net/vyyXP/1/
the form html tag usually contains another attribute called action. You usually use this if you want the data from the form to go to some other page. Since we don't want to create another page we can either omit this altogether or use something like:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"></form>
This is the only place your PHP_SELF comes in. Again, if you know something else about it, let me know.
So.. now lets actually start solving this problem of yours now.
Step 1: Figure out how to get and use the button clicks.
Every time you click a button. The form submits and the page reloaded. If you have knowledge about headers, it'll add an extra pressed=1 or pressed=2, etc every time you click a button and submit the form. This 'pressed' keyword comes from the fact that our buttons have name="pressed" as an attribute. (Notice that all buttons have the same name!).
We will need to use this data and figure out what the person actually clicked.
Since I'm using <form method="post"> you can get this data by using <?php $pressed = $_POST['pressed'] ?> alternatively if you used method="get" you would have had to use $_GET['pressed'] instead.
As an exercise just display an 'X' on whichever button the user clicks on. You should dynamically generate the html code for the buttons inside the form to make your life easier.
<?php
for ($i=0; $i<9; $i++) {
echo '<button name="pressed" value="'.$i.'">';
if ($_POST['pressed']==$i)
echo 'X';
echo '</button>';
if ($i!=0 && ($i+1)%3==0)
echo "</br>\n";
}
?>
Step 2: Figure out how to remember data.
Since you're using php. I'd recommend that you use php sessions. You basically just need to put session_start() in the beginning of the file and then you can store values into the $_SESSION variable and php will remember then next time you visited the page. You can use this to count how many times a button has been pressed so that you can alternate between O's and X's.
Figure out how to destroy sessions. (Make that reset button work). This is pretty important.
You can also use cookies or write to a file if you want but sessions are probably easiest thing to use.
Here is an example of a simple counter.
<?php
session_start();
$count = 0;
if (isset($_SESSION['count']))
$count = $_SESSION['count'];
echo $count;
$_SESSION['count'] = $count+1;
?>
And.. you're done!
This is probably everything important that you need to do. The rest is just implementing how the game tic-tac-toe works. Let me know if you need any clarifications on anything I have written or if you need more information. Hope this helps!