I have the following code which is causing me problem. The code generates two buttons, one to remove the selected project and the other to cancel the deletion of the project. The remove function works well, however I haven't found a way to make my cancel button redirect to my url (when I click on it, nothing happens). Any clue?
...
echo "<form method='post'>";
echo "<input type='hidden' value='".$currentid."' name='project'/>";
echo "<b>".$project_name."</b>";
?>
<div class="btn_2"> <input type="submit" name="save" value="Remove"><input type="submit" onclick="window.location.replace('www.myurl.ca')" value="Cancel"></div>
</form>
You're using a submit button and tagging JS on to it. I'm expecting that you're actually submitting the form, but I could be wrong.
If nothing is happening with your JS, you may try doing window.location.href = "www.myurl.ca"; return false; where including the return false should avoid the submit.
The best solution would probably be to make it a plain button and wrap it in an A tag. You would avoid submitting your form, redirect, and all without requiring JS be active.
...
echo "<form method='post'>";
echo "<input type='hidden' value='".$currentid."' name='project'/>";
echo "<b>".$project_name."</b>";
?>
<div class="btn_2"> <input type="submit" name="save" value="Remove"><button type="button">Cancel</button></div>
</form>
Using the formaction button attribute works.
<input type="submit" name="save" value="Cancel" formaction="http://www.myurl.ca">
Related
I want to show a button in my page only if a certain condition is met.
Also i want to run a query (DELETE QUERY) when i press that button.
<?php if (isset($_POST['finduser_btn']) && $noerr) :
echo "<div class='green'>
<button type='submit' class='btn' name='scoreDel'>Delete scores</button>
</div>
endif ?>
I use $noerr as FLAG to display or not the button if another button is pressed (the other button is not shown in code)
Well, how do i use scoreDel button to run a query like:
DELETE FROM scores
WHERE name = '$username$;
I think i have some issue with " and ' into the PHP echoing html tags but i'm not sure... I hope in some help, i'm getting mad.
Thanks in advance
You need a form in order to submit your action.
echo '<form action="mypage.php" method="POST"><div class="green">
<button type="submit" class="btn" name="scoreDel">Delete scores</button>
</div></form>';
Try the following:
<?php if (isset($_POST['finduser_btn']) && $noerr) : ?>
<div class='green'>
<form method="post">
<input type="text" name="finduser">
<button type='submit' class='btn' name='scoreDel'>Delete scores</button>
</form>
</div>
<?php endif ?>
Use a form to submit the button tag. Also, write html outside of PHP code if possible.
I have a search form on my page.
<form action='' method='post'>
<input type='text' name='search' />
<input type='sumit' name='submit' value='submit'/>
</form>
When the user clicks the submit button on the form, it should run a mysql_query and create a link to the user page.
if(isset($_POST['search'])){
$add = "city = {'$_POST['search']'}";
}
$res = mysql_query("SELECT * FROM user WHERE {$add}");
while($rw=mysql_fetch_object($res)){
echo "<a href=user.php?id={$rw->user_id}?>{$rw->name}</a>";
}
When I click on the link user.php?id=3, it goes to the user page and everything is OK. But I have problem when I click the browser's back-button, on user.php page. Then i have problem back to previous page.
Confirm Form Resubmission.
Correct the following:
<input type='sumit' name='submit' value='submit'/>
You are writing type='sumit' instead of type='submit'
To not display the "Confirm Form Resubmission" alert, do you have to change your submission (method) to GET.
<form action='' method='GET'>
<input type='text' name='search' />
<input type='sumit' name='submit' value='submit'/>
</form>
...
if(isset($_GET['search'])){
$res = mysql_query(sprintf("SELECT * FROM user WHERE city='%s'", mysql_real_escape_string($_GET['search']) ));
while($rw=mysql_fetch_object($res)){
echo "<a href=user.php?id={$rw->user_id}?>{$rw->name}</a>";
}
}
You should use get method instead of post. Post resubmissions must be confirmed by most browsers.
P.s.: you shouldn't pass the user input into your sql query directly to prevent the risk of sql injections.
I have made a few radio buttons in my php code
echo "<input type='radio' name='rad' value='a'>";
echo "<input type='radio' name='rad' value='b'>";
and my html code is
<form method="post" action="fetch_page_4.php">
<input type="submit" name="submit1" value="Positive" id="btn1"/>
<input type="submit" name="submit2" value="Negative" id="btn2"/></form>
Whenever i click the Positive or Negative button it should go into the respective part in PHP...which it does. But the problem is that i need to find out which of my radio buttons is checked and take the action accordingly.
But i am unable to do so. My code for it is
if(isset($_POST['submit1']))
{
$main_key=0;
if(isset($_POST['rad']) && ($_POST['rad'])=='a')
{
$sub_arr=explode(" ",$array_subject[0]);
$count_sub=count($sub_arr);
$body_arr=explode(" ",$array_message[0]);
$count_msg=count($body_arr);
$main_key=1;
}
}
If i echo the value of "main_key" after this...it is coming out to be 0, thus indicating that it is not going inside the loop even when the radio button is checked and has the required value.
Can somebody please tell me why is this happening?
is there a way (without using JS) to get into PHP the value of pressed
<button type='button' name='bobby' value='".$value."'> .. </button>?
In latter code, I have the submit button as well (to submit all form).
As from what I've experienced before, PHP doesn't work. JavaScript is the bet way to set the value. You cannot get the value of the button in a form btw.
You can use to find the form submission isset and $_POST
if(isset($_POST['bobby'])){
echo "You pressed the button";
}else{
echo "No action triggered yet";
}
HTML:
<button type='submit' name='bobby' value='<?php echo $value;?>'></button>
it has error because $_POST['sub1'] can't be accessed
is there any approach or solution to echo the value of $_POST['sub1']? or impossible? no way? even with another arrays?
i had question about my code nobody solved it! then I decide to tell it in simple way.
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
I think I know what is wrong here.
When you submit the form the second time (for sub2) you are no longer posting the value of sub1 along with it, just sub2.
This should fix it:
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='hidden' name='sub1' value='" . htmlentities($_POST['sub1']) . "'>";
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
You're using submit buttons. Only the button that you actually click will have its name/value pair sent to the server. When you click the sub2 button, only sub2=sub2 is sent, so sub1 won't exist in the $_POST array.
followup:
$_POST is created for you by PHP based on what's sent from the browser. The way you've built your form makes it impossible for 'sub1' to exist when you click the 'sub2' button. In other words, you need to use the SAME name= for BOTH buttons, and change the value= as appropriate:
html:
<input type="submit" name="submit" value="sub1" />
<input type="submit" name="submit" value="sub2" />
php:
if (isset($_POST['submit'])) {
echo "You clicked the {$_POST['submit']} button";
}