I have succeeded displaying data from query from mysql. But there is a url that connected to files that has been stored in mysql database. But I need to changes this url that text based to a button. Here is my script:
<td align="center"><a href='<?php echo $res['url_new_edition'] ?>'>New Edition</a></td>
Now I want to changes the 'New Edition' texts of course as a link/url into a button. I was already tried to add 'class=button', type='button' ect but none of works.
try this
<input type="button" onclick="window.location.href='http://google.com'; return false;">
in your case
<input type="button" onclick="window.location.href='<?php echo $res['url_new_edition'] ?>'; return false;">
Use a form instead of a href if you want a button. A clear solution that even comes along without JavaScript would look like this:
<form action="<?php echo $res['url_new_edition']; ?>" method="get">
<input type="submit" value="New Edition" />
</form>
Related
I'm currently working on at the displaying of information from a database. I was making a summary site where you can only see the important things of a table. After that i made the first element as an <input type="submit"> in a <form>, so u can click it and come to the detail site. My problem is now: The value of this input type has to be my ID, so i can query correctly on me detail site. I was wondering if it is possible to use something like a placeholder, so that the ID is the value, but on the input type is written other text.
My input:
<form method="post" action="Details.php">
<input type="submit" placeholder = "test" name="Overview" onclick="parent.location='Details.php'" value="<?php echo $data[$i1]; ?>">
</form>
How it currently looks
I want it that the input type still has the same value, but is displaying on the website something else like "test".
Greetings!
No, but buttons can have different values and labels.
<button name="foo" value="bar">baz</button>
Since you are using a form-tag per row, you can add a hidden input-field in the form and set the value of the submit-button to whatever you like.
<form method="post" action="Details.php">
<input type="hidden" name="id" value="<?php echo $data[$i1]; ?>" />
<input type="submit" name="Overview" value="test" />
</form>
I have a form on an HTML/PHP page.
I have the same exact code on other pages on the site, and it works fine. I cannot figure it out.
Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
<input name="ContestEntry" type="submit" class="submit" value="Enter me in the Raffle!" />
</form>
I looked at the HTML source code, and the action populates the correct page, and $prize_id populates the correct info in the value.
PHP:
if(isset($_POST['ContestEntry'])){
//...code to enter data into form, irrelevent since it won't post anything anyway.
}
else {
echo 'Nothing posted from form';
}
"Nothing posted from form" always shows, and no data is being entered into the database. I've tried changing the form name to 5 different things, thinking maybe there was a conflicting name somewhere, but nothing works.
Any ideas?
If all you need to achieve is check whether the form is submitted or not, it's better to check that the request type is a POST:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Process form
} else {
// Nothing posted!
}
Also change your submit button to:
<button type="submit">Submit</button>
and see what happens
The code you posted actually works for me.
But, if you still can't get it working, I would try checking to see if prize_id is set rather than the button. The PHP script is executed when the form is submitted.
Also, I would recommend that you don't use $_SERVER['PHP_SELF'] as the form action. According to https://stackoverflow.com/a/14093363/3593228, that can make it easy for attackers to insert malicious data. Instead, leave the action empty.
The if Condition is not working because you put it on Button, instead of this just make an hidden field in the form to check form is submit or not like as you already have one. Or make new for this form.
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
Consider adding what you're looking for as another hidden field:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
<input name="ContestEntry" type="hidden" value="Enter me in the Raffle!" />
<input type="submit" class="submit" />
</form>
I have a search form that refreshes the page when the submit button is hit with the table of results below. this works fine and the url is http://example.com/?txtKeyword=searchterm I want it to add &sku=123456789 to the end of the url I know to add the sku from my database I need to use this term $_GET[sku]. I have tried to add this to the form and I have been unsuccessful. Here is the form code:
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME']?>">
<table width="100%">
<tr>
<th>Search: (Keyword/SKU)
<input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>" size="40">
<input class="alert button" type="submit" value="Search"></th>
</tr>
</table>
</form>
I think I need to add this line:
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME']'&sku='$_GET["sku"]?>">
This does not work. I am not too sure, trying to learn PHP and I cannot seem to find the problem or a solution. Am I doing this wrong and what would I need to do to get this right?
It Depends if your script is looking for a $_POST['sku'] or $_GET['sku'] when you process it.
If it is a POST then you add a new hidden <input>
<input type="hidden" name="sku" value="XXXXX">
adding it to the form action will only work if it looks for $_GET['sku']
EDIT
If you wish to echo something if its there but not if it isn't then do something like this.
<?php echo !empty($_GET['sku']) ? '<input type="hidden" name="sku" value="'.$_GET['sku'].'">' : ""; ?>
Here I used JavaScript to Delete an employee...
<script type="text/javascript">
function delet(emp)
{
var answer = confirm('Are you sure?');
if(answer)
{
window.location='delete.php?emp='+emp;
}
}
</script>
By using anchor tag am calling the function of javascript ....
<a onclick="javascript:delet('<? echo $_GET['emp']; ?>')">
<input type="button" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value=""/></a>
But my problem is it is working upto showing the alert msg but after answering the alert msg it is not redirecting to the page given by me that is "delete.php?emp=+emp"
Because you're nesting an <input type="Submit"/> inside an <a>, clicking the button is not the same as clicking the <a>, rather it "Submits" nowhere (read to the page you're already on, refreshing the page) before the <a> can do it's job.
Simple demo where you don't go to google.
<input type="Submit"/>
Bergi has pointed out that the behaviour I described is not universal (hello Opera, IE) unless a <form> element is present, so for example the following
<form action="jail.php">
<a href="go.php" onclick="window.location='?collect=£200';">
<input type="submit"/>
</a>
</form>
will send you directly to jail without passing go or collecting £200 across all browsers.
Further, this only really applies to page redirection; other pieces of script may well fire before the page changes, the easiest to observe being console.log, alert, etc.
You're submitting some form by clicking on that button. That submit process will overrule the window.location navigation request. A quick workaround would be to prevent that:
<a onclick="event.preventDefault(); delet('<? echo $_GET['emp']; ?>');">
// cross-browser-safe:
<a onclick="delet('<? echo $_GET['emp']; ?>'); return false">
but actually your markup with the nested button is quite odd. You don't need that link at all, just use
<form action="delete.php" onsubmit="return confirm('Are you sure');">
<input type="hidden" name="emp" value="<? echo $_GET['emp']; ?>" />
<input type="submit" name="delete" id="delete" style="background: url('images/del1.jpg')no-repeat;width:50px;height:50px" value="" />
</form>
I'm very interested in if it is possible to connect javascript variables to php. I know that in php we can write javascript code, but on the contrary we could not. To express my aim better , lets bring example like that:
<form name="some" action="<?php $_SERVER['php_self']; ?>" method="post">
<input type="submit" name="but" value="Action">
</form>
My question is how to make after pressing submit button to confirm (alert) with javascript and if it is confirmed do something (with php) and if isn't cancel (php operation).
You can do two things to pass javascript variable to php:
You can pass it as a hidden input field and submit it using POST
<input id="myHidden" name="myHidden" type="hidden"/>
Assign javascript variable to hidden input something like
var myVariable;
document.getElementById("myHidden").value = myVariable;
You can pass it as a query string with in your URL
As for confirming you can use javascript confirm, which will post on OK and not post/cancel on CANCEL
Something like:
<input type="submit" onclick="return confirm('Are you sure you want to submit?')"/>
<form name="some" action="<?php $_SERVER['php_self']; ?>" method="POST"
onsubmit="document.getElementById("response")
= confirm('some question') ? 'yes' : 'no';
return true;">
<input type="hidden" name="response" id="response" value="">
<input type="submit" name="but" value="Action">
</form>
BTW action="<?php $_SERVER['php_self']; ?>" opens up your page to XSS attacks.