Can I set $_GET[] without "Submit"?

Hi everyone,
I have a form in “test.php” which I want to process in “test1.php”.
test.php is the following:

<!DOCTYPE html>
  <title>Update Records In MYSQL Database Using PHP</title>
  <!--connecting to database-->
      while($row = mysqli_fetch_array($records))
        echo "<tr><form action = 'test1.php' method = 'GET'>";
        echo "<td><input type = 'text' name = 'mkt_id' value = '".$row['id']."'></td>";
        echo "<td><input type = 'text' name = 'mkt_name' value = '".$row['name']."'></td>";
        echo "<td><a href='test1.php?edit_task=".$row['id']."'>edit</a></td>";
        echo "</form></tr>";

I can change the data in test’s text boxes as I wish but do the changes take place?
Because “test1.php” when activated by clicking the link, shows the original values !

  echo $_GET["edit_task"];

I think that if I sent data wherby a “Submit” button’ changes would take place but I try to do it without a Submit button.
Is that possible ? How?
Thanks !

HTTP GET is only meant to retrieve data. Any sort of mutation on the server side (storing stuff to the database for example) must be done in a POST request.

Saving data without a submit button can be done using AJAX, but if you’re not sure if your current code works I’d make sure it works first before going any further.

First make it work, then make it pretty.

1 Like

Your <a href> link just takes you directly to test1.php without any of the form contents, because it has nothing to do with the form. The fact that you displayed it inside the form is neither here nor there, it’s just a hyperlink.

As @rpkamp said, you can do it with Ajax. You could also use CSS to style your submit button, or use an image or something else.

1 Like

From the Title of the Thread:
Can you set $_GET? Yes. $_GET is a malleable array.
Should you? No.

From the actual post:
What you’re trying to do is doable without AJAX, but it’s a Javascript manipulation still.
It would be easier with AJAX instead, and manipulating a button’s onclick event, but strictly from what the OP has asked, it can be done.

What he’s trying to do is the following (pseudocode):

on input change:
   this.parent.find('a').href = 'test1.php'+urlEncode(this.parent)
1 Like

Thank you very much.
Ajax and JS will wait for a while and I’ll use what you kindly advise for the moment.

Make a function to check everything you want has been set, and then if it has, submit the form:

function submitIfFormComplete()
  // Check the select has something selected
  if (document.getElementById('selectOne').selectedIndex > 0)

Then on your select, bind the onchange event to run the function.

Select your car make:
<select id='sel1' name='selectCar' onchange="checkAndSubmit()">
    <option value="0">Select...</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
Select your gender:
<select id='sel2' name='selectGender' onchange="checkAndSubmit()">
    <option value="0">Select...</option>
  <option value="Male">Volvo</option>
  <option value="Female">Saab</option>


function checkAndSubmit()
  if (document.getElementById('sel1').selectedIndex > 0
     && document.getElementById('sel2').selectedIndex > 0)
      alert('both have been selected!');

I’ve replaced the submit with an alert() to show you how the code triggers.

Edit: You can use $_REQUEST['selectCar'] to access the value.

Thanks for the JS script. I’d rather stick to HTML/PHP for the time being.

PHP is incapable of doing what you want to do.

PHP runs before the page loads. Once it hands the data to the user’s client, it’s done until the user sends another request to the server.

Any reaction to something the user does (fill in a box, move the mouse, whatever) that does not involve sending a request back to the server is Javascript’s domain.

1 Like

There seems to be some confusion between GET and POST.

Forms have “submit” and typically send POST arrays. Links can have query strings that send GET arrays, but links are HTTP requests, not submits.

Anyway, I’m guessing the question is if it’s possible to do something like

if( !isset($_GET['something']) ) { 
  $_GET['something'] = "somevalue"; 
$stmt = bindParam(':someplaceholder', $_GET['something'], PDO::PARAM_INT);

IMHO, though possible, it is much better to do it more like this

if( !isset($_GET['something']) ) { 
  $somename = "somevalue";
} else { 
  $somename = my_custom_filter_function($_GET['something']);
$stmt = bindParam(':someplaceholder', $somename, PDO::PARAM_INT);
1 Like

Thanks a lot !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.