whatagoal1 Posted January 27, 2014 Share Posted January 27, 2014 hi I have a form which resides on an html page, when the user enters data within the form this data is used to build a href and also database infomation. The submit button instigates a php form which saves the data to a database. WORKS PERFECT BRILLIANT!!!!! As of last week I had never heard of php or javascript so am pretty pleased with myself. If I then change the submit button to use a javacript function to navigate to the href it , WORKS PERFECT BRILLIANT!!!!! What I want is to be able for Both of these to be activated on the click of the submit button, been trying for over 10 hours and no success, so hopefully maybe someone can help. Quote Link to comment https://forums.phpfreaks.com/topic/285722-from-html-form-to-php-and-then-javascript/ Share on other sites More sharing options...
gizmola Posted January 27, 2014 Share Posted January 27, 2014 Code or even a jsfiddle would be pretty helpful. Quote Link to comment https://forums.phpfreaks.com/topic/285722-from-html-form-to-php-and-then-javascript/#findComment-1466779 Share on other sites More sharing options...
Psycho Posted January 27, 2014 Share Posted January 27, 2014 Yeah, your request is a little vague. But, my guess as to what you have stated is that you can have the Submit button post the form to a PHP page to do the database updates or you can have it use JavaScript to redirect to a URL, but you are having trouble accomplishing both. You can certainly do both by having JavaScript post the data to one page and then having the JavaScript redirect to another. But, in my opinion, that is entirely unnecessary. JavaScript is great, but I think it gets overused. I think you can accomplish what you are after by using the Submit button normally. Then on the page that receives the POST data, perform all the functionality you are doing now - THEN use a header() function in the PHP code to do the redirect. Then again, you may just include() the other page. Without understanding the workflow it's difficult to say what would be best. For example, let's say you are on a form to add a user. And, when the user submits the form, you want the data to be saved to the database and then take the user to the page that lists all the users. If it was something like this, then you can have the new user form simple submit to the displayUsers.php page. Then on that page you could have some logic such as this: if(isset($_POST['username'])) { //Include php script to process form data include('addUser'); } //Continue with code to display users Quote Link to comment https://forums.phpfreaks.com/topic/285722-from-html-form-to-php-and-then-javascript/#findComment-1466782 Share on other sites More sharing options...
whatagoal1 Posted January 27, 2014 Author Share Posted January 27, 2014 (edited) firstly thanks for your time both, and I apologise foe my lack of clarity. If I may try again. I need for 2 actions on the user click of the submit button which resides on an html page within a form. Although it matters not which action is performed first, both need to be executed. i also am opening the href in a separate window completely independent of my website(don't think this matters to the execution) for gizmola, will try and copy the code and post Edited January 27, 2014 by whatagoal1 Quote Link to comment https://forums.phpfreaks.com/topic/285722-from-html-form-to-php-and-then-javascript/#findComment-1466784 Share on other sites More sharing options...
whatagoal1 Posted January 27, 2014 Author Share Posted January 27, 2014 html <form action="insert.php" method="post"> <textarea id="text1" name="ff1" rows="1">Monday 11 february</textarea> <textarea id="text2" name="ff2" >Selected item is</textarea> <input type="submit" "> </form> php $sql="INSERT INTO mysaver (ddate, iitem) VALUES ('$_POST[ff1]','$_POST[ff2]')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); javascript function mynew(){ var s1=document.getElementById("text1").value; var s2=document.getElementById("text2").value; var answer =s1+s2 alert(answer) return answer } Quote Link to comment https://forums.phpfreaks.com/topic/285722-from-html-form-to-php-and-then-javascript/#findComment-1466786 Share on other sites More sharing options...
Psycho Posted January 28, 2014 Share Posted January 28, 2014 Your JavaScript function doesn't have anything that would open a new page - or you didn't provide the right javascript. What you can do is change your FORM tag to have an onsubmit trigger that will call the JavaScript to open the new page. That function should have a "return true;" at the end - assuming everything was good and you want the page to open. The FORM tag would look like this: <form action="insert.php" method="post" onsubmit="return doSomthing();" > When the user submits the form, it will first call the doSomething() function. Then, if a true response is returned, the form will POST to the page listed in the action parameter. Quote Link to comment https://forums.phpfreaks.com/topic/285722-from-html-form-to-php-and-then-javascript/#findComment-1466813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.