Dumb_Shock
Dumb_Shock

Reputation: 1008

Multiple Submit Button PHP

I have written something like this in PHP

<script type="text/javascript" src="jquery.js"></script></script>
<script type="text/javascript">
 function get(){
$.post('output.php', { name: form.name.value },
function(output) {
    $('#age').html(output).show();

}) ;

}

</script> 

<body> 
<p>
<form name="form">
id:
<input type ="text" name="name"><input type ="button" name="Submit_1" value ="Get          
Existing Data" onclick="get();">//gets info from DB

<p>url: <input type="text" name="calc_url"/></p>
<option value="abc">abc</option> 
</select><br/>
<input type="submit" name="Submit_2" value="submit" />
</form>
<div id="title"></div>   
</p>
</body>
</html>

Output.php

<?php
Connect to DB

$name = mysql_real_escape_string($_POST['name']);


if(isset($_POST['submit1'])){
if ($name==NULL)
echo "please enter an id!";
else
{   
$age= mysql_query("SELECT title FROM parentid WHERE id ='$name'");
$age_num_rows = mysql_num_rows($age);   
  if ($age_num_rows==0)
  echo "id does not exist";

 else
{
 $sql ="SELECT * FROM parentid WHERE id = '$name'";       
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$abc_output= "Existing data of the record <br />";      
$abc_output .="Title: " . $row['title'] . "<br />" ;
$abc_output .="Report No: " .  $row['reportno'] . "<br />" ;
 $abc_output .="URL: " .  $row['calc_url'] . "<br />" ; 
$abc_output .="Institution: " .  $row['institution'] . "<br />" ; 
 }
}
echo $abc_output;
}
}
 if (isset($_POST['Submit_2'])) {

$value = mysql_real_escape_string($_POST['name']);
$value1 = mysql_real_escape_string($_POST['title']);
$sql = " UPDATE parentid SET title='$value1' WHERE id ='$value'";
if (!mysql_query($sql)) 
{
die ('Error:' .mysql_error());

}
}



?>

This is what i have done and it does not work!! But if the above is done individually i.e.,without if (isset()) it works(single button considered individually)

Upvotes: 0

Views: 2598

Answers (1)

Dor
Dor

Reputation: 7484

You can detect which submit button has been pressed by checking for it's existence:

if (isset($_POST['Submit_2'])) {
  // "Submit_2" submit button was clicked
}

Edit:

  1. Your HTML syntax is incorrect.
  2. Write if(isset($_POST['name']) and not if(isset($_POST['submit1']), since the parameter name is name, indicated by the second parameter of the $.post() jquery's method: $.post('output.php', { name: form.name.value },
  3. The documentation of jquery.post tells us that the query string (in either POST or GET request) is built from the second parameter of that method. Which means that the $_POST superglobal will have only one key-value pair. Test that by writing print_r($_POST) at the top of your PHP script.
  4. It's not recommended to use deprecated Javascript methods, e.g.: form.name.value. Instead, fetch the value of a DOM object by referring to it's id (best approach). For instance if your input button has an <input ... id="button1" />, then fetch it's value with $('#button1').val()
  5. Try something like this:

    <form>
    <input type="text" name="name" value="">
    <input type="button" name="submit1" value="Submit1" id="g1" />
    <input type="button" name="submit2" value="Submit2" id="g2" />
    </form>  
    <script type="text/javascript" src="jquery.js"></script></script>
    <script type="text/javascript">
    $('input[name^="submit"]').click(function() {
        submit_name = $(this).attr('name')
        submit_value = $(this).val()
        $.post('output.php', { submit_name: submit_value },
        function(output) {
            // do something
        });
    });
    </script> 
    

    There was never really a submission of the form, but it was sent via ajax, thus there's no input of type submit.

Upvotes: 7

Related Questions