Jump to content

from html form to php AND then javascript


whatagoal1

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by whatagoal1
Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.